我尝试使用传统方式修改ttk.Combobox
小部件字体
text_font = ('Courier New', '10')
mycombobox = Combobox(font = text_font)
mycombobox.pack()
但字体并没有真正改变......
我也使用ttk.Style
,但没有任何反应......
text_font = ('Courier New', '10')
ttk_style = ttk.Style()
ttk_style.configure('App.TCombobox', font=text_font)
mycombobox = Combobox(style = "App.TCombobox")
mycombobox.pack()
有没有办法控制字体?我想更改Entry
和ListBox
字体
答案 0 :(得分:1)
这是一种非常奇怪的行为,因为它在我身边很有效:
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
import random
import string
def insert_something_to_combobox(box):
box['values'] = [gen_key() for _ in range(10)]
def gen_key(size=6, chars=string.ascii_uppercase + string.digits):
# just to generate some random stuff
return ''.join(random.choice(chars) for _ in range(size))
root = tk.Tk()
text_font = ('Courier New', '10')
main_frame = tk.Frame(root, bg='gray') # main frame
combo_box = ttk.Combobox(main_frame, font=text_font) # apply font to combobox
entry_box = ttk.Entry(main_frame, font=text_font) # apply font to entry
root.option_add('*TCombobox*Listbox.font', text_font) # apply font to combobox list
combo_box.pack()
entry_box.pack()
main_frame.pack()
insert_something_to_combobox(combo_box)
root.mainloop()
也可以指定特定组合框的字体,因为我们可以依赖ttk::combobox::PopdownWindow函数:
...
class CustomBox(ttk.Combobox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bind('<Map>', self._handle_popdown_font)
def _handle_popdown_font(self, *args):
popdown = self.tk.eval('ttk::combobox::PopdownWindow %s' % self)
self.tk.call('%s.f.l' % popdown, 'configure', '-font', self['font'])
...
root = tk.Tk()
text_font = ('Courier New', '10')
main_frame = tk.Frame(root, bg='gray') # main frame
combo_box = CustomBox(main_frame, font=text_font) # apply font to combobox
entry_box = ttk.Entry(main_frame, font=text_font) # apply font to entry
...
root.mainloop()
但是,这个CustomBox
缺少功能,因为一旦组合框架小部件被配置了popdown的字体,因此任何以后的字体配置都不会为弹出窗口配置此选项。
让我们尝试覆盖默认配置方法:
class CustomBox(ttk.Combobox):
def __init__(self, *args, **kwargs):
# initialisation of the combobox entry
super().__init__(*args, **kwargs)
# "initialisation" of the combobox popdown
self._handle_popdown_font()
def _handle_popdown_font(self):
""" Handle popdown font
Note: https://github.com/nomad-software/tcltk/blob/master/dist/library/ttk/combobox.tcl#L270
"""
# grab (create a new one or get existing) popdown
popdown = self.tk.eval('ttk::combobox::PopdownWindow %s' % self)
# configure popdown font
self.tk.call('%s.f.l' % popdown, 'configure', '-font', self['font'])
def configure(self, cnf=None, **kw):
"""Configure resources of a widget. Overridden!
The values for resources are specified as keyword
arguments. To get an overview about
the allowed keyword arguments call the method keys.
"""
# default configure behavior
self._configure('configure', cnf, kw)
# if font was configured - configure font for popdown as well
if 'font' in kw or 'font' in cnf:
self._handle_popdown_font()
# keep overridden shortcut
config = configure
这个类将产生一个响应更快的组合框实例。