sedeh
sedeh

Reputation: 7313

AttributeError: Object has no attribute 'listbox

I am trying to combine optionmenu and listbox to guide selection from a dictionary. Everything appears to be working fine. However, I am getting AttributeError: 'App' object has no attribute 'listbox'. Any idea why this error? Also, is it possible to use a spinbox in place of the 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 my code:

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()

Using Python 3.4.1

Upvotes: 1

Views: 1724

Answers (1)

Andrew Johnson
Andrew Johnson

Reputation: 3186

Your updateoptions method references self.listbox though it may be called before self.listbox is defined.

Move

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

to somewhere below where self.listbox is defined

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

Upvotes: 1

Related Questions