微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何修改ttk Combobox字体?

如何解决如何修改ttk Combobox字体?

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

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的字体,因此,字体的任何后续配置都不会为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

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

解决方法

我正在尝试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()

有没有办法控制字体?我想同时更改EntryListBox字体

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。