繁体   English   中英

Tkinter OptionMenu:如何配置下拉列表的字体大小?

[英]Tkinter OptionMenu: How to configure font size of drop down list?

我有一个选项菜单列表,其中包含许多要在触摸屏设备上使用的条目。 I am able to change the font size of the selected category with PopMenue.config(font=[something]) , but when selecting the drop down menu the entries appear in the default small font:

示例图片 - 红框围绕文本以增加字体

使用比选项菜单本身更小的字体的选项菜单的下拉菜单的屏幕截图

如何从下拉菜单条目(红框)中修改字体大小?

代码片段:

helv36 = tkFont.Font(family='Helvetica', size=30, weight=tkFont.BOLD)
popupMenu.config(font=helv36)

您必须使用nametowidget()方法获取与下拉菜单小部件对应的小部件对象,然后设置配置。

这是一个可运行的示例:

import tkinter as tk
import tkinter.font as tkFont

root = tk.Tk()
root.geometry('300x200')

helv36 = tkFont.Font(family='Helvetica', size=36)
options = 'eggs spam toast'.split()
selected = tk.StringVar(root, value=options[0])

choose_test = tk.OptionMenu(root, selected, *options)
choose_test.config(font=helv36) # set the button font

helv20 = tkFont.Font(family='Helvetica', size=20)
menu = root.nametowidget(choose_test.menuname)  # Get menu widget.
menu.config(font=helv20)  # Set the dropdown menu's font
choose_test.grid(row=0, column=0, sticky='nsew')

root.mainloop()

这是两个屏幕截图,显示了默认与修改后的下拉菜单文本大小:

下拉菜单截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM