python - AttributeError: Object has no attribute 'listbox -


i trying combine optionmenu , listbox guide selection dictionary. appears working fine. however, getting attributeerror: 'app' object has no attribute 'listbox'. idea why error? also, possible use spinbox in place of 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' 

here's code:

from tkinter import * 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         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')         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() 

using python 3.4.1

your updateoptions method references self.listbox though may called before self.listbox defined.

move

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

to somewhere below self.listbox defined

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

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -