python - Python Tkinter - How to update Combobox values depending on OptionMenu Selection?
问题描述
I'm looking for some help with the following.
I'm working on a small project that requires the ComboBox values to be updated depending on the selection the user makes in an OptionMenu.
Currently the Combo Box shows the values for Thread 1 but for most of the time it shows a value like PY with a number (i.e. PY_VAR2)
Below is the main section of the code from these two widgets I'm trying to connect.
Thanks in advance for your help.
### 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)
Combo Box Section
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'])
Thread 1
已经在 OptionMenu 中选择了但 Combobox 呈现TPIVals
,无论它们可能是什么,这让我有点困扰。
推荐阅读
- reactjs - 如何使用 gl-react-native 实现这种效果?
- ruby - 格式化程序“Teamcity::Cucumber::Formatter”正在使用已弃用的格式化程序 API
- redis - [vertx.redis.client]没有处理程序等待消息
- obiee - OBIEE 12c:饼图中的重叠标签
- python - 根据字典中的存在过滤numpy数组
- java - 如何使用 Apache Flink 从 Cassandra 读取数据?
- java - Chronicle:反序列化期间未初始化对象
- php - 在 Laravel 中通过内部 with() 排序
- scala - Gatling 将数据从 feeder 传递到 exec
- ios - UIView 弹簧动画是否使用缓动曲线选项?