在Python 3中使用tkinter更改单选按钮的背景颜色

时间:2016-05-15 02:51:13

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

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

我已尝试过' background =','前景=',' bg =',' fg =&# 39;以及ttk.Radiobutton()中的一些其他内容。它适用于标签和其他东西......我缺少什么?

1 个答案:

答案 0 :(得分:2)

ttk在其Radiobutton上不支持诸如“background”,“foreground”,“font”之类的参数,但它确实支持样式。 示例代码(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