簡體   English   中英

Python Tkinter-如何根據OptionMenu選擇更新組合框值?

[英]Python Tkinter - How to update Combobox values depending on OptionMenu Selection?

我在以下方面尋求幫助。

我正在做一個小型項目,該項目需要根據用戶在OptionMenu中所做的選擇來更新ComboBox值。

當前,“組合框”顯示線程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)

組合框部分

thdInitTPI = StringVar()
thdTPICombo = ttk.Combobox(thdParamsFrame, width = 17, textvariable=thdInitTPI, values=TPIVals)

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'])

thdTPICombo.bind('<<ComboboxSelected>>',None)

好吧,您從OptionMenu中有一個回調: thdTypeSelection所以只需在那里更新Combobox:

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'])

令我有些困擾的是,雖然已經在OptionMenu中選擇了Thread 1 ,但是組合TPIVals顯示了TPIVals ,無論它們是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM