Python Tkinter - 选项数据库仅部分工作
Python Tkinter - Option database only partially working
我正在制作一个深色主题的 GUI,并希望通过使用选项数据库设置默认参数来节省时间,但只有部分选项有效。 "background" 和 "font" 之类的选项可以正常工作,但更改单选按钮的活动背景或选择颜色等其他选项不起作用。更改条目小部件上的 "insertbackground" 也不起作用。
我不确定为什么某些选项有效而其他选项无效。如果我在创建小部件时传入相同的参数,它就可以工作。例如:
myRadio = Radiobutton(frame, selectcolor='#FF0000', **otherKwargs)
这两种方式我都试过了。
import tkinter
from tkinter import Tk, ttk, Frame, Entry, Label, Button, Toplevel, BooleanVar, IntVar, Radiobutton, StringVar, Canvas
mw = Tk()
mw.option_readfile('StyleDatabase.txt')
# code here...
mw.mainloop()
"StyleDatabase.txt" 文件包含以下内容:
*background: #000008
*foreground: grey90
*font: '', 11
*Entry*background: #404050
*Radiobutton*selectcolor: #FF0000 <---- This one doesn't work.
# also tried:
*selectcolor: #FF0000
*Radiobutton.selectcolor: #FF0000 <---- Neither worked
我也试过使用 mw.option_add() 函数,none 以下方法有效:
mw.option_add('*selectbackground', 'blue')
mw.option_add('*Radiobutton*selectbackground', 'blue')
mw.option_add('*Radiobutton.selectbackground', 'blue')
据我所知,没有任何其他方法可以做到这一点。
编辑:这是一个演示问题的可行脚本,Python 3.x
import tkinter
from tkinter import Tk, ttk, Frame, Entry, Label, Button, Toplevel, BooleanVar, IntVar, Radiobutton, StringVar, Canvas
class MainUI:
def __init__(self, master):
self.master = master
Label(self.master, text='Hello').pack(side='top')
Entry(self.master).pack(side='top')
Entry(self.master, insertbackground='red').pack(side='top')
Button(self.master, text='Cyan Button').pack(side='top')
mw = Tk()
mw.option_add('*background', 'blue') # works
mw.option_add('*foreground', 'white') # works
mw.option_add('*Button.foreground', 'cyan') # works
mw.option_add('*insertbackground', 'red') # does not work. Works if i pass the argument in when creating the widget.
mw.option_add('*Entry.insertbackground', 'red') # also does not work
mw.option_add('*Entry*insertbackground', 'red') # also does not work
mainUI = MainUI(mw)
mw.mainloop()
Question: Option database only partially working
您受困于 tkinter
和底层 Tcl
使用的不同选项名称和别名。
你的选项参数必须是,注意大写Color
:
*Radiobutton*selectColor: #FF0000
你可以从.config
得到.option_readfile
或.option_add
使用的名字:
myRadio = tk.Radiobutton(frame, selectcolor='#FF0000', **otherKwargs)
myRadio.grid()
print('{}'.format(myRadio.config()['selectcolor']))
>>> ('selectcolor', 'selectColor', 'Background', '#ffffff', '#FF0000')
这个答案 解释了五个值中的每个选项。
The Second name, here 'selectColor'
, are used by the option database.
Note: Options not shown from <widget instance>.config()
can not be set.
测试 Python:3.5 - 'TclVersion':8.6 'TkVersion':8.6
我正在制作一个深色主题的 GUI,并希望通过使用选项数据库设置默认参数来节省时间,但只有部分选项有效。 "background" 和 "font" 之类的选项可以正常工作,但更改单选按钮的活动背景或选择颜色等其他选项不起作用。更改条目小部件上的 "insertbackground" 也不起作用。
我不确定为什么某些选项有效而其他选项无效。如果我在创建小部件时传入相同的参数,它就可以工作。例如:
myRadio = Radiobutton(frame, selectcolor='#FF0000', **otherKwargs)
这两种方式我都试过了。
import tkinter
from tkinter import Tk, ttk, Frame, Entry, Label, Button, Toplevel, BooleanVar, IntVar, Radiobutton, StringVar, Canvas
mw = Tk()
mw.option_readfile('StyleDatabase.txt')
# code here...
mw.mainloop()
"StyleDatabase.txt" 文件包含以下内容:
*background: #000008
*foreground: grey90
*font: '', 11
*Entry*background: #404050
*Radiobutton*selectcolor: #FF0000 <---- This one doesn't work.
# also tried:
*selectcolor: #FF0000
*Radiobutton.selectcolor: #FF0000 <---- Neither worked
我也试过使用 mw.option_add() 函数,none 以下方法有效:
mw.option_add('*selectbackground', 'blue')
mw.option_add('*Radiobutton*selectbackground', 'blue')
mw.option_add('*Radiobutton.selectbackground', 'blue')
据我所知,没有任何其他方法可以做到这一点。
编辑:这是一个演示问题的可行脚本,Python 3.x
import tkinter
from tkinter import Tk, ttk, Frame, Entry, Label, Button, Toplevel, BooleanVar, IntVar, Radiobutton, StringVar, Canvas
class MainUI:
def __init__(self, master):
self.master = master
Label(self.master, text='Hello').pack(side='top')
Entry(self.master).pack(side='top')
Entry(self.master, insertbackground='red').pack(side='top')
Button(self.master, text='Cyan Button').pack(side='top')
mw = Tk()
mw.option_add('*background', 'blue') # works
mw.option_add('*foreground', 'white') # works
mw.option_add('*Button.foreground', 'cyan') # works
mw.option_add('*insertbackground', 'red') # does not work. Works if i pass the argument in when creating the widget.
mw.option_add('*Entry.insertbackground', 'red') # also does not work
mw.option_add('*Entry*insertbackground', 'red') # also does not work
mainUI = MainUI(mw)
mw.mainloop()
Question: Option database only partially working
您受困于 tkinter
和底层 Tcl
使用的不同选项名称和别名。
你的选项参数必须是,注意大写Color
:
*Radiobutton*selectColor: #FF0000
你可以从.config
得到.option_readfile
或.option_add
使用的名字:
myRadio = tk.Radiobutton(frame, selectcolor='#FF0000', **otherKwargs)
myRadio.grid()
print('{}'.format(myRadio.config()['selectcolor']))
>>> ('selectcolor', 'selectColor', 'Background', '#ffffff', '#FF0000')
这个答案
The Second name, here
'selectColor'
, are used by the option database.
Note: Options not shown from
<widget instance>.config()
can not be set.
测试 Python:3.5 - 'TclVersion':8.6 'TkVersion':8.6