问题描述
我正在尝试使用传统方式修改 ttk.Combobox
小部件字体
I've trying modifing the ttk.Combobox
widget fonts using the traditional way
text_font = ('Courier New', '10')
mycombobox = Combobox(font = text_font)
mycombobox.pack()
但字体并没有真正改变...
but the font doesn't really change...
我也尝试使用 ttk.Style
但同样没有任何反应......
I also tring using ttk.Style
but again nothing happens...
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
字体
Is there a way to control the fonts? I want to change both the Entry
and the ListBox
fonts
推荐答案
这真的很奇怪,因为它在我这边效果很好:
It's really strange behavior, because it's works well on my side:
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 函数:
It's also possible to specify a font for a particular combobox, since we can rely on ttk::combobox::PopdownWindow function:
...
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 的字体就被配置了,因此任何以后的字体配置都不会为 popdown 配置这个选项.
However, this CustomBox
lacks functionality, because popdown's font is configured once combobox widget is mapped, hence any later configuration of the font won't configure this option for the popdown.
让我们尝试覆盖默认配置方法:
Let's try to override default configuration method:
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
这个类将产生一个更具响应性的组合框实例.
This class will produce a more responsive instance of the combobox.
这篇关于如何修改 ttk Combobox 字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!