简体   繁体   English

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

[英]Python 2.7 Tkinter how to change text color of a button's text

button1 = Button(root,text='Revert image',foreground = "red",compound="center") button1 =按钮(root,text ='Revert image',foreground =“red”,compound =“center”)

This type of code is not working. 这种类型的代码不起作用。 it says unknown option "-foreground". 它说未知的选项“-foreground”。

This is the whole code that works - 这是有效的整个代码 -

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()

Because you are doing global imports (rarely ever a good idea), and because you import ttk after tkinter. 因为你正在进行全局导入(很少有好主意),并且因为你在tkinter之后导入了ttk。 Both libraries define a Button widget, so the ttk Button is overriding the tkinter Button . 两个库都定义了一个Button小部件,因此ttk Button会覆盖tkinter Button The ttk Button has no foreground option. ttk Button没有foreground选项。

You should stop using global imports to eliminate this problem: 您应该停止使用全局导入来消除此问题:

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

so taking a look right here you can see that the "foreground" and "fg" option is the same. 所以看看这里你可以看到“前景”和“fg”选项是一样的。 But this is only the case in the new version of tkinter for python3, if you are using an older version for python2.7 you have to use the "fg" option. 但是这只是python3的新版tkinter中的情况,如果你使用旧版本的python2.7,你必须使用“fg”选项。

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

If you would like to change the text color afterwards you can achieve this by using the config function: 如果您想在之后更改文本颜色,可以使用config函数实现此目的:

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

I hope this clears things up a bit. 我希望这可以解决一些问题。 keep coding ;D 保持编码; D.

I use fg 我用fg

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

edit: hm, actually, both fg and foreground work for me. 编辑:嗯,实际上, fgforeground都适合我。 If you don't bother with the color, does everything else work? 如果你不打扰颜色,其他一切工作吗? It may be that some other error is propagating down. 可能是某些其他错误正在传播下来。 Here is an example of a simple Hello World program using tkinter. 以下是使用tkinter的简单Hello World程序示例。 See if it works for you. 看看它是否适合你。 I think the capitalization of tkinter has changed between Python 2 and 3. This is for Python 3. 我认为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()

Try this in Python 2.7: 在Python 2.7中试试这个:

import Tkinter as tk 将Tkinter导入为tk

so Tkinter with capitalized T 所以Tkinter用大写的T.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM