tkinter 中的组合框字体大小 [英] Combobox fontsize in tkinter

查看:100
本文介绍了tkinter 中的组合框字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ttk Combobox 创建一个带有选项的下拉列表.这样做时,我可以配置传递给它的默认值的字体大小.但是当我单击箭头时,其他值的字体大小保持不变.我正在为触摸屏开发应用程序,所以我需要提供适当的大小.这是示例代码,当我运行代码时,A 的大小更大,单击箭头键时,我看到其他值是默认大小.

Hi I am trying to use the ttk Combobox to create a dropdown with options . While doing so i can configure the font size of the default value passed to it . But when i click the arrow the font size of the other values remains the same .I am developing the app for touchscreen , so i need to provide proper size . Heres the sample code , when i run the code the size of A is bigger , button the on clicking the arrow key i see the other values are of default size .

#! /usr/bin/python

from Tkinter import *
import ttk


class Application:

    def __init__(self, parent):
        self.parent = parent
        self.combo()

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value,font=("Helvetica",20))
        self.box['values'] = ('A', 'B', 'C')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.mainloop()

推荐答案

问题是 ttk Combobox 的下拉菜单实际上是一个简单的 Tkinter Listbox 所以它不受 ttk 样式的影响.如果可以从组合框获得对列表框的引用,则更改字体将很容易.但是,我在 Tkinter 中找不到这样做的方法.

The thing is that the dropdown menu of the ttk Combobox is actually a simple Tkinter Listbox so it isn't affected by the ttk style. If it would be possible to get a reference to the Listbox from the Combobox, changing the font would be easy. However, I couldn't find a way to do so in Tkinter.

按照 patthoyts' 进行编辑,非常有用评论.
您可以做的是使用

Edited as per patthoyts' very useful comment.
What you can do is change the font for all Listboxes that are part of a Combobox using

bigfont = tkFont.Font(family="Helvetica",size=20)
root.option_add("*TCombobox*Listbox*Font", bigfont)

这会更改属于 ttk Combobox 的所有 Listbox 小部件的字体,这些小部件是在调用此之后创建的.
这确实会影响所有 新组合框,但我认为这就是您想要的.如果你只想要这个 Combobox 的新字体,你可以选择创建这个 Combobox 作为最后一个小部件并在之前调用 self.parent.option_add("*TCombobox*Listbox*Font", bigfont)创建此组合框.那么只有这个 Combobox 下的 Listbox 才会有新字体.

That changes the font of all Listbox widgets that are part of a ttk Combobox and that are created after calling this.
This does affect all new Comboboxes, but I assume that's what you want. If you want the new font only for this Combobox, you could choose to create this Combobox as the last widget and call self.parent.option_add("*TCombobox*Listbox*Font", bigfont) right before creating this Combobox. Then only the Listbox under this Combobox will have the new font.

如果你想让所有小部件都拥有更大的字体,你可以使用

If you want all widgets to have the bigger font, you can use

root.option_add("*Font", bigfont)

或者您可以更改默认字体,如此答案中所述.

or you can change the default font as described in this answer.

这篇关于tkinter 中的组合框字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆