更多关于tkinter选项菜单首选项消失的问题
这是我之前提问的后续内容,链接在这里。我想用ttk.OptionMenu来改善下拉菜单的外观和感觉。但是在这篇帖子中提到,“ttk选项菜单一开始会显示所有的值在下拉框里。选择任何一个值后,列表中的第一个值就会消失,再也不会出现了……” 解决这个问题的方法是往列表里添加一个空项。在我的情况下,因为我使用的是字典,所以在字典的第一项添加'':[]
解决了这个问题。这是唯一的解决方案吗?我不想在我的字典里加一个多余的东西。以下是代码:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class App:
def __init__(self, master):
master.title("Continental System")
self.dict = {'':[], 'Asia': ['Japan', 'China', 'Malasia'],
'Europe': ['Germany', 'France', 'Switzerland'],
'Africa': ['Nigeria', 'Kenya', 'Ethiopia']}
self.frame1 = ttk.Frame(master)
self.frame1.pack()
self.frame2 = ttk.Frame(master)
self.frame2.pack()
self.variable_a = tk.StringVar()
self.variable_b = tk.StringVar()
self.variable_a.trace('w', self.updateoptions)
self.optionmenu_a = ttk.OptionMenu(self.frame1, self.variable_a, *self.dict.keys())
self.variable_a.set("Asia")
self.optionmenu_a.grid(row = 0, column = 0)
self.optionmenu_b = ttk.OptionMenu(self.frame1, self.variable_b, '')
self.optionmenu_b.grid(row = 0, column = 1)
self.btn = ttk.Button(self.frame2 , text="Submit", width=8, command=self.submit)
self.btn.grid(row=0, column=1, padx=20, pady=20)
def updateoptions(self, *args):
countries = self.dict[self.variable_a.get()]
self.variable_b.set(countries[0])
menu = self.optionmenu_b['menu']
menu.delete(0, 'end')
for country in countries:
menu.add_command(label=country, command=lambda country=country: self.variable_b.set(country))
def submit(self, *args):
var1 = self.variable_a.get()
var2 = self.variable_b.get()
if messagebox.askokcancel("Confirm Selection", "Confirm your selection: " + var1 + ' ' + var2 + ". Do you wish to continue?"):
print(var1, var2)
def set_window(self, *args):
w = 800
h = 500
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root = tk.Tk()
app = App(root)
app.set_window()
root.mainloop()
另外,我还收到了几个错误信息,包括AttributeError: 'App' object has no attribute 'optionmenu_b'
,这个问题在我上面提到的第一个问题的回答中似乎已经解决了。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "C:\Python34\tk1_version2.py", line 32, in updateoptions
menu = self.optionmenu_b['menu']
AttributeError: 'App' object has no attribute 'optionmenu_b'
Python版本 3.4.1
1 个回答
4
你引用的那篇帖子是错的。你不需要在列表的第一个项目前加一个空格。ttk.OptionMenu
命令的格式要求在变量名后面第一个参数是一个默认值。
你不需要在字典里添加一个空项。你在创建选项菜单时确实需要添加一个默认值:
self.optionmenu_a = ttk.OptionMenu(self.frame1, self.variable_a, "Asia", *self.dict.keys())
一个稍微更好的解决办法是获取键的列表并把它保存到一个列表中。对这个列表进行排序,然后用列表的第一个元素作为默认值:
options = sorted(self.dict.keys())
self.optionmenu_a = ttk.OptionMenu(self.frame1, self.variable_a, options[0], *options)
你之所以会遇到错误,是因为你在这个变量上设置了一个跟踪,它调用了一个引用self.optionmenu_b
的函数,但你在创建self.optionmenu_b
之前就已经设置了这个变量。简单的解决办法是先创建第二个菜单。
self.optionmenu_b = ttk.OptionMenu(...)
self.optionmenu_a = ttk.OptionMenu(...)