Python 2.7 Tkinter 如何更改按钮文本的文本颜色

viv*_*vkv 5 python text tkinter colors button

Button1 = Button(root,text='恢复图像',前景=“红色”,compound=“中心”)

这种类型的代码不起作用。它说未知选项“-foreground”。

这是完整的代码 -

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()
Run Code Online (Sandbox Code Playgroud)

Bli*_*get 1

我用的是fg

button1 = tk.Button(root, text='hello', fg='red')
Run Code Online (Sandbox Code Playgroud)

编辑:嗯,实际上,两者fgforeground为我工作。如果你不关心颜色,其他一切都可以吗?可能是其他一些错误正在向下传播。这是使用 tkinter 的简单 Hello World 程序的示例。看看它是否适合你。我认为 tkinter 的大小写在 Python 2 和 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()
Run Code Online (Sandbox Code Playgroud)