栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何修改ttk Combobox字体?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何修改ttk Combobox字体?

这确实是一种奇怪的行为,因为在我这方面效果很好:

try:    import tkinter as tk    import tkinter.ttk as ttkexcept importError:    import Tkinter as tk    import ttkimport randomimport stringdef 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 framecombo_box = ttk.Combobox(main_frame, font=text_font)    # apply font to comboboxentry_box = ttk.Entry(main_frame, font=text_font)       # apply font to entryroot.option_add('*TCombobox*Listbox.font', text_font)   # apply font to combobox listcombo_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 framecombo_box = CustomBox(main_frame, font=text_font)       # apply font to comboboxentry_box = ttk.Entry(main_frame, font=text_font)       # apply font to entry...root.mainloop()

但是,这

CustomBox
缺乏功能,因为一旦组合框窗口小部件映射后就配置了popdown的字体,因此,字体的任何后续配置都不会为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

此类将产生一个响应更快的组合框实例。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号