Python2.7Tkinter如何更改按钮文本的颜色

2024-06-02 00:03:50 发布

您现在位置:Python中文网/ 问答频道 /正文

button1=按钮(根,text=”还原图像”,foreground=”红色”,compound=”中心“)

这类代码不起作用。上面写着未知的选项“前景”。

这就是整个密码-

from Tkinter import *
from ttk import *
def change():
   label.config(text="Hey dude !!")
   label.config(image = img1,background='blue',foreground='yellow')
def click():
         if button1.instate(["disabled"]):
                label.config(image = img1,background='yellow',foreground='green')
                button1.state(['!disabled'])
                button.state(['disabled'])
         else:
                label.config(image = img,background='yellow',foreground='green')
                button1.state(['disabled'])
                button.state(['!disabled'])
root = Tk()
label = Label(root)
img=PhotoImage(file='C:\\Users\\Vivek\\Desktop\\x.gif')
img1= PhotoImage(file='C:\\Users\\Vivek\\Desktop\\y.gif')
img2 = PhotoImage(file='C:\\Users\\Vivek\\Desktop\\z.gif')
button = Button(root)
button.pack()
button1 = Button(root,text='Revert image',compound="center")
img2_small = img2.subsample(30,80)
button.config(image=img2_small,text='Change image',compound='center')
button1.state(["disabled"])
button1.pack()
label.pack()
button.config(command=click)
button1.config(command = click)
label.config(image = img,background='yellow',foreground='green')
label.config(text = 'Hey dude watsup ?? Are you in a need help ?')
label.config(compound = 'left',wraplength=100,font=('Courier',20,'bold'))
label.after(5000,change)
root.mainloop()

Tags: textimageconfigbuttonrootlabelimg1background
3条回答

我用fg

button1 = tk.Button(root, text='hello', fg='red')

编辑:嗯,实际上,fgforeground都对我有用。如果你不在乎颜色,其他的都行吗?可能是其他一些错误正在向下传播。下面是一个使用tkinter的简单Hello World程序的示例。看看是否对你有用。我认为tkinter的大小写在Python2和3之间发生了变化。这是针对Python 3的。

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.TVB1 = tk.StringVar(self, value='hello, there')

        B1 = tk.Button(self)
        # this is an example of how you can define parameters after
        # defining the button
        B1["textvariable"] = self.TVB1
        B1["command"] = self.say_hi
        B1.grid(row=0,column=0)

        self.TVE1 = tk.StringVar(self, value='wubwub')
        E1 = tk.Entry(self, textvariable=self.TVE1)
        E1.grid(row=1, column=0)

        # and this is how you can define parameters while defining
        # the button
        Quit = tk.Button(self, text='QUIT', fg='red',
                              command=self.master.destroy)
        Quit.grid(row=2,column=0)

    def say_hi(self):
        print(self.TVB1.get())
        self.TVB1.set(self.TVE1.get())


root = tk.Tk()
app = Application(root)
app.mainloop()

所以向右看here你可以看到“前景”和“前景”选项是一样的。但这仅适用于python3的新版本tkinter,如果您使用的是python2.7的旧版本,则必须使用“fg”选项。

btn = Button(root, fg='red') #Creates a button with red text

如果您想在之后更改文本颜色,可以使用config函数来实现此目的:

btn.config(fg='blue') #Changes the text color to blue

我希望这能澄清一些事情。 继续编码;D

因为您正在进行全局导入(很少是一个好主意),并且因为您在tkinter之后导入ttk。两个库都定义了一个Button小部件,因此ttkButton正在重写tkinterButton。ttkButton没有foreground选项。

应停止使用全局导入来消除此问题:

import Tkinter as tk
import ttk
...
root = tk.Tk()
...
tk.Button(...)

相关问题 更多 >