更改 tkinter 组合框中选择部分中值的字体大小

Changing the font size of the values in the selection section in the combobox in tkinter

当我在 tkinter 中制作一个组合框并单击箭头指向 select 一个值时,这些值的字体大小非常小。当我更改字体大小时,输入框中文本的字体大小会变大,但 selection 部分中值的字体大小保持不变。如何更改 selection 部分中值的大小?

from tkinter import *
from tkinter import ttk

WIDTH, HEIGHT = 1000, 300

root = Tk()

root.geometry(f"{WIDTH}x{HEIGHT}")

def createDropBox(master, dropBoxData, fg, bg, fontSize):
    ttk.Style().configure("style1.TCombobox", foreground = fg, background = bg)
    dropBox = ttk.Combobox(master, values = dropBoxData, style = "style1.TCombobox", font = ("", fontSize), width = 60)
    return dropBox

values = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
dropBox = createDropBox(root, values, "blue", "white", 20)
dropBox.place(x = 20, y = 20)

root.mainloop()

您可以添加一个 tkinter 选项来增加组合框中列表框的字体。在 root.mainloop():

之前将其添加到您的代码中
from tkinter.font import Font
font = Font(family = "Helvetica", size = 20)
root.option_add("*TCombobox*Listbox*Font", font)

我在更改 所有 tkInter 小部件使用的字体大小时遇到​​了很多麻烦(它们对于我的高分辨率屏幕来说都太小了)。最终我发现这段代码可以做到:

import tkFont
tk = Tkinter.Tk()
for f in tk.splitlist(tk.call("font", "names")):
    tkFont.nametofont(f).configure(size=10)

您需要在创建任何 tkInter 小部件之前执行此操作。如果您只想更改一些字体大小,请打印 tk.splitlist(tk.call("font", "names")) 的结果并尝试一次更改它们以查看您的哪些小部件使用。