Python Tkinter如何根据选项菜单选择更新组合框值?

2024-09-29 19:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在以下方面寻求帮助。在

我正在做一个小项目,它需要根据用户在选项菜单中所做的选择来更新组合框值。在

当前,组合框显示线程1的值,但在大多数情况下,它显示的值类似于带有数字的PY(即PY_VAR2)

下面是我尝试连接的这两个小部件的代码的主要部分。在

提前谢谢你的帮助。在

### Option Menu Section
thdTypeLabel = Label(thdParamsFrame, text="Thread Type")
thdTypeLabel.grid(row=0, column=0, padx=(30,10), pady=(10,10),sticky=E)

thdInitType = StringVar(thdParamsFrame)
thdInitType.set("Thread 1")
thdTypeMenu = OptionMenu(thdParamsFrame, thdInitType, "Thread 1","Thread 2", "Thread 3", command=thdTypeSelection)
thdTypeMenu.grid(row=0, column=1)
thdTypeMenu.configure(width=14)

组合框部分

^{pr2}$

Tags: 项目用户py选项菜单column线程thread
1条回答
网友
1楼 · 发布于 2024-09-29 19:17:59

好吧,您有一个来自OptionMenu的回调:thdTypeSelection所以只需在那里更新组合框:

def thdTypeSelection(event=None):
    thdType = thdInitType.get()
    if thdType == "Thread 1":
        thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
    elif thdType == "Thread 2":
        thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
    elif thdType =="Thread 3":
        thdTPICombo.config(values=['6','7','8','10','11','12','14','16','18','20'])

让我有点困扰的是Thread 1已经在选项菜单中被选中了,但是组合框显示了TPIVals,不管它们是什么。在

相关问题 更多 >

    热门问题