简体   繁体   English

如何更新 ttk.OptionsMenu 中的下拉选项(python,tkinter,ttk)

[英]How to update the dropdown options in ttk.OptionsMenu (python, tkinter, ttk)

I have found plenty of documentation on how to update a OptionsMenu object using tkinter but due to various issues revolving around my version of mac OS, I have been using ttk to build my project and I've found myself stumped when it comes to updating the options menu.我找到了大量关于如何使用 tkinter 更新 OptionsMenu object 的文档,但是由于围绕我的 mac OS 版本的各种问题,我一直在使用 ttk 来构建我的项目,我发现自己在更新选项菜单。

My options are coming from a list which is added using the following code:我的选项来自使用以下代码添加的列表:

dropdown_menu = ttk.OptionMenu(root, dropdown_menu_text, dropdown_options[0], *dropdown_options)

and I am using a separate method to update the contents of the dropdown_options variable.我正在使用单独的方法来更新dropdown_options变量的内容。 According to the information I found, updating the optionsmenu using standard tk involves essentially clearing the options, then looping through my updated list and adding each option back again.根据我发现的信息,使用标准 tk 更新选项菜单基本上涉及清除选项,然后循环浏览我更新的列表并再次添加每个选项。

Is it a similar kind of logic with ttk?是不是和ttk有类似的逻辑?

Turns out after some more digging around I was able to find the solution myself and it was far simpler than I anticipated.事实证明,经过更多的挖掘,我自己找到了解决方案,而且比我预期的要简单得多。 Turns out the answer is simply to add the line原来答案只是添加行

dropdown_menu.set_menu(dropdown_options[0], * dropdown_options) 

Assuming that dropdown_options is your updated list!假设 dropdown_options 是您的更新列表!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM