gpt4 book ai didi

python - 属性错误: Object has no attribute 'listbox

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:43 25 4
gpt4 key购买 nike

我正在尝试结合选项菜单和列表框来指导从字典中进行选择。一切似乎都运行良好。但是,我得到 AttributeError: 'App' object has no attribute 'listbox' 。知道为什么会出现这个错误吗?另外,是否可以使用旋转框代替列表框?

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:/Users/Sam/Desktop/dict.py", line 40, in updateoptions
self.listbox.delete(0, 'end')
AttributeError: 'App' object has no attribute 'listbox'
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:/Users/Sam/Desktop/dict.py", line 40, in updateoptions
self.listbox.delete(0, 'end')
AttributeError: 'App' object has no attribute 'listbox'

这是我的代码:

from tkinter import *
from tkinter import ttk
import tkinter.messagebox

class App:
def __init__(self):
self.master = Tk()
self.di = {'Asia': ['Japan', 'China', 'Malaysia', 'India', 'Korea',
'Vietnam', 'Laos', 'Thailand', 'Singapore',
'Indonesia', 'Taiwan'],
'Europe': ['Germany', 'France', 'Switzerland'],
'Africa': ['Nigeria', 'Kenya', 'Ethiopia', 'Ghana',
'Congo', 'Senegal', 'Guinea', 'Mali', 'Cameroun',
'Benin', 'Tanzania', 'South Africa', 'Zimbabwe']}
self.variable_a = StringVar()
self.frame_optionmenu = ttk.Frame(self.master)
self.frame_optionmenu.pack()
self.variable_a.trace('w', self.updateoptions)
options = sorted(self.di.keys())
self.optionmenu = ttk.OptionMenu(self.frame_optionmenu, self.variable_a, options[0], *options)

self.variable_a.set('Asia')
self.optionmenu.pack()
self.btn = ttk.Button(self.master, text="Submit", width=8, command=self.submit)
self.btn.pack()

self.frame_listbox = ttk.Frame(self.master)

self.frame_listbox.pack(side=RIGHT, fill=Y)
self.scrollbar = Scrollbar(self.frame_listbox )
self.scrollbar.pack(side=RIGHT, fill=Y)
self.listbox = Listbox(self.frame_listbox, selectmode=SINGLE, yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox.yview)
self.listbox.pack()

#Populate listbox
for each in self.di[self.variable_a.get()]:
self.listbox.insert(END, each)
self.listbox.bind("<Double-Button-1>", self.OnDouble)

self.master.mainloop()

def updateoptions(self, *args):
countries = self.di[self.variable_a.get()]
self.listbox.delete(0, 'end')
for each in self.di[self.variable_a.get()]:
self.listbox.insert(END, each)
self.listbox.pack()

def submit(self, *args):
var = self.variable_a.get()
if messagebox.askokcancel("Selection", "Confirm selection: " + var):
print(var)

def OnDouble(self, event):
widget = event.widget
selection = widget.curselection()
value = widget.get(selection[0])
print(value)

App()

使用Python 3.4.1

最佳答案

您的 updateoptions 方法引用 self.listbox,尽管它可能会在定义 self.listbox 之前调用。

移动

self.variable_a.trace('w', self.updateoptions)

到下面定义self.listbox的地方

self.listbox = Listbox(self.frame_listbox, selectmode=SINGLE, yscrollcommand=self.scrollbar.set)    
self.variable_a.trace('w', self.updateoptions)

关于python - 属性错误: Object has no attribute 'listbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25374355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com