Take the 2-minute tour ×

I'm on to programming a graphical frontend in Python 2.7.3 using Tkinter. I've got a main menu (A in the example) and a window (B). A contains a listbox, B is doing something which needs the content of the listbox in A. After B has finished its work, I need a method in A (doSomething) called. My simplified code looks like this:

#!/usr/bin/env python

import Tkinter as tk

class A(object):
    def __init__(self, root):
        self.__mainMenu = root
        self.__LB = tk.Listbox(self.__mainMenu)
        self.__LB.insert(tk.END, "foo")
        b = B(self.__mainMenu, self.__LB)

    def doSomething(self):
            print "Ham and spam!"

class B(object):
    def __init__(self, mainMenu, LB):
        self.__mainMenu = mainMenu
        self.__LB = LB
        print self.__LB.get(0)

def main():
    root = tk.Tk()
    gui = A(root)


The following output is produced:

$ ./myTest.py 
Traceback (most recent call last):
  File "./myTest.py", line 29, in <module>
  File "./myTest.py", line 26, in main
    gui = A(root)
  File "./myTest.py", line 11, in __init__
    b = B(self.__mainMenu, self.__LB)
  File "./myTest.py", line 21, in __init__
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __getattr__
    return getattr(self.tk, attr)
AttributeError: doSomething

Where does this error come from? Why is the error output so poor?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I always use gtk (no experience with Tk), but from what I see, you invoke doSomething method of root, not of A class, in B.init.__mainMenu.doSomething()
Invoke B with self in A init:

b = B(self, self.__LB)
share|improve this answer
I'm so ashamed... It was late at night! :-) Thanks, that solved it. –  user1488553 Feb 28 '13 at 9:42

Your Answer


