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

Nat*_*n R 2 tkinter colors radio-button ttk python-3.x

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

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

Ste*_*ric 9

ttk 的单选按钮不支持诸如“背景”、“前景”、“字体”等参数,但它支持样式。示例代码(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
Run Code Online (Sandbox Code Playgroud)