【问题标题】:Changing the background color of a radio button with tkinter in Python 3在 Python 3 中使用 tkinter 更改单选按钮的背景颜色
【发布时间】:2016-09-11 01:42:50
【问题描述】:

我在使用 ttk 在 Python 3 中处理的 GUI 中添加了几个单选按钮,但它们周围有一个白色方块,与 GUI 其余部分的蓝色背景不匹配。

我在ttk.Radiobutton() 中尝试过background= ...foreground= ...bg= ...fg= ... 以及其他一些东西。它适用于标签和其他东西......我错过了什么?

【问题讨论】:

    标签: python-3.x colors tkinter radio-button ttk


    【解决方案1】:

    ttk 在其 Radiobutton 上不支持诸如“背景”、“前景”、“字体”之类的参数,但它确实支持样式。 示例代码(python 3.4):

    from tkinter import *
    import tkinter.ttk as ttk
    
    
    root = Tk()                         # Main window
    myColor = '#40E0D0'                 # Its a light blue color
    root.configure(bg=myColor)          # Setting color of main window to myColor
    
    s = ttk.Style()                     # Creating style element
    s.configure('Wild.TRadiobutton',    # First argument is the name of style. Needs to end with: .TRadiobutton
            background=myColor,         # Setting background to our specified color above
            foreground='black')         # You can define colors like this also
    
    rb1 = ttk.Radiobutton(text = "works :)", style = 'Wild.TRadiobutton')       # Linking style with the button
    
    rb1.pack()                          # Placing Radiobutton
    
    root.mainloop()                     # Beginning loop
    

    【讨论】:

    • 像魅力一样工作。谢谢!
    猜你喜欢
    • 2020-10-09
    • 2013-12-04
    • 2011-10-03
    • 2011-06-11
    • 2015-01-03
    • 2023-04-10
    • 1970-01-01
    • 2016-09-10
    • 2013-03-21
    相关资源
    最近更新 更多