【问题标题】:How to modify ttk Combobox fonts?如何修改 ttk Combobox 字体?
【发布时间】:2017-08-22 12:28:56
【问题描述】:

我正在尝试使用传统方式修改 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 字体

【问题讨论】:

  • 之前asked in 2015也有类似的问题,并在那里得到了解答。

标签: python python-3.x tkinter combobox


【解决方案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 缺少功能,因为一旦映射了组合框小部件,就会配置弹出窗口的字体,因此任何以后的字体配置都不会为弹出窗口配置此选项。

让我们尝试覆盖默认配置方法:

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

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

【讨论】:

  • 您的代码确实有效 - 主要区别在于 root.option_add('*TCombobox*Listbox.font', text_font) 语句在我的应用程序中为所有组合框操作列表框字体。这同样有效,但会破坏我的应用程序中的其他 GUI 元素。
  • @NirMH 是的,我知道。所以你的问题特别是关于列表框字体。但是,in accordance to ttk.combobox documentation 组合框利用 pre-ttk 列表框作为其下拉元素,因此当前需要“选项”命令来设置列表框选项。所以我认为这不能用python来改变只指定的combolistbox,但他们用tcl展示了一种方式!唉,我没能用tkevalcall 这个命令。
  • 只有我一个人,还是这一定是人们进入Tkinter 的重要时刻啊?。在那之前,一切看起来都很有希望。
  • @o'rety,我用特定组合框的解决方案稍微更新了我的答案。还是大佬呵呵吗?
  • @CommonSense:我不想让这个评论部分被删除并转移到聊天中,所以我只想说它不再是递归的,因为你在此期间更正了它(有一个在第 8 行致电 combo_box 而不是 self)。至于我的问题,我很确定这不是由我的代码中的任何特殊性引起的,并且确信这是一种常见的情况。现在,回到主题 - 您的解决方案有效,不能将其合并到官方 Tkinter 代码中吗?更改 ttk.Combobox 上的字体就可以了,未来的用户无需在 SO 上寻求帮助。
猜你喜欢
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
相关资源
最近更新 更多