【问题标题】:Python 2.7 Tkinter how to change text color of a button's textPython 2.7 Tkinter 如何更改按钮文本的文本颜色
【发布时间】:2016-01-01 09:21:29
【问题描述】:

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

这种类型的代码不起作用。它说未知选项“-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()

【问题讨论】:

    标签: python button text colors tkinter


    【解决方案1】:

    我用fg

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

    编辑:嗯,实际上,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()
    

    【讨论】:

    • 还是没有运气。它说 - NameError: name 'tk' is not defined.
    • 如果我不尝试使用 fg/foreground 更改颜色,一切正常。
    • 在上面的示例中,我将 tkinter 作为 tk 导入,这是使用 tkinter 等库时的标准做法。不要冒犯,但我会假设你对 tkinter 有点陌生。按钮在 tkinter 之外不存在,因此除非您将库中的所有内容导入命名空间,否则您发布的代码 sn-p 将不起作用,这是不好的做法。考虑粘贴您拥有的精简代码示例,该示例无需指定颜色即可工作。
    • 就像我说的那样,将整个库导入到您的命名空间中是一种不好的做法。我将在我的答案中添加一个示例程序,看看它是否适合你。
    • 是的,你的程序在 python3.1 上工作,但我如何在 python2.7 上使用它?
    【解决方案2】:

    在 Python 2.7 中试试这个:

    将 Tkinter 导入为 tk

    所以 Tkinter 带有大写的 T

    【讨论】:

    • Python 2.7 没有名为“tkinter”的模块,它是“Tkinter”
    • 在 Python 2 中,tkinter 就是 Tkinter。你导入什么并不重要。要遵循标准格式,您可以使用“import Tkinter as tk”
    【解决方案3】:

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

    你应该停止使用全局导入来消除这个问题:

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

    【讨论】:

    • 谢谢 :) 但是为什么会发生这个问题呢?所以我导入了整个库这意味着我的代码效率不好但是问题背后的原因是什么?
    • @Vivekkumarpathak:问题背后的原因是这两个库各自定义了Button,但它们是不同的。一次支持foreground,一个不支持。
    【解决方案4】:

    所以看一下here,你可以看到“前景”和“fg”选项是相同的。但这仅适用于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

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2021-11-12
      • 2013-06-05
      • 2019-02-14
      • 1970-01-01
      相关资源
      最近更新 更多