有没有办法调整组合框条目的大小?

时间:2018-12-14 19:26:07

标签: python tkinter combobox

我知道有很多方法可以更改下拉列表/列表框字体,我的问题是如何同时调整输入字段(红色圆圈部分)。如何使其尺寸与旁边的标签相同?换句话说,我如何增大下拉箭头

label = ttk.Label(frame, text='truck id: ', anchor=w, font=bigger_font)
c = ttk.Combobox(frame, textvariable=truckID, values=['1','2','3','4'])
c['state'] = 'readonly'
root.option_add('*TCombobox*Listbox.font', bigger_font)

enter image description here

1 个答案:

答案 0 :(得分:2)

Combobox具有 width 属性,可用于控制其大小。 width以字符数表示。因此,例如,如果您知道组合框条目是一位数字,则可以将width属性设置为1。这是一个示例。

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
tList = ttk.Combobox(root, values=[1, 2, 3, 4, 5], state="readonly", width=1)
tList.current(0)
tList.grid(row=0, column=1, padx=10, pady=10)
root.mainloop()

enter image description here

现在看看,如果您将宽度更改为2。

enter image description here

它恰好是组合框条目的一半。

基本上,如果您知道组合框将包含的条目类型(长度),则可以控制其大小。

如果您希望它的高度更高,请操纵其font属性。

tList = ttk.Combobox(root, values=[1, 2, 3, 4, 5], state="readonly", width=2, font="Verdana 16 bold")

enter image description here