【问题标题】:How do I color more a button, with different colors in tkinter ttk.button?如何在 tkinter ttk.button 中使用不同颜色为按钮着色?
【发布时间】:2019-10-03 05:07:56
【问题描述】:

我想给按钮上色,但目前我发现只能一次性上色。

我在ttk.button() 中尝试了bg = "red",但它返回了以下错误:

/_tkinter.TclError: 未知选项“-bg”

style = ttk.Style()
style.configure("TButton",foreground="red")

#Botoes para funcoes da treeview
ttk.Button (text='Deletar', command = self.delete).grid(row=5,column=0, sticky = W + E)
ttk.Button (text='Editar', command = self.editar).grid(row=5,column=1, sticky = W + E)

希望你能帮助我,我将不胜感激。

【问题讨论】:

    标签: python button tkinter colors


    【解决方案1】:

    你不能在ttk.Style() 中使用 bgfg 短形式的 backgroundforeground 你已经使用完整的词 backgroundforeground 来配置样式。

    tkinter.TclError: 未知选项“-bg”

    您遇到的错误是因为您无法将 -bg 作为参数传递给ttk.Button()。要配置任何 ttk 小部件的样式,您必须使用 ttk.Style 及其受人尊敬的样式名称,例如 Button : "TButton", Label : "TLabel", Frame : "TFrame" 等等,请参阅documentation 的 Tk 主题小部件。

    要为不同的按钮创建单独的样式,您可以创建自定义样式名称。

    例如:

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    
    button1_style = ttk.Style() # style for button1
    # Configure the style of the button here (foreground, background, font, ..)
    button1_style.configure('B1.TButton', foreground='red', background='blue')
    button1 = ttk.Button(text='Deletar', style='B1.TButton')
    button1.pack()
    
    button2_style = ttk.Style() # style for button2
    # Configure the style of the button here (foreground, background, font, ..)
    button2_style.configure('B2.TButton', foreground='blue', background='red')
    button2 = ttk.Button(text='Editar', style='B2.TButton')
    button2.pack()
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2018-06-10
      • 2017-12-19
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      相关资源
      最近更新 更多