0

I've got following problem: In my main tk window I have a button which opens a new Toplevel window. Once a button on the Toplevel window is clicked, a new element to a listbox should be added.

I keep getting this error message:

Traceback (most recent call last):
  File "MainWindow.py", line 4, in <module>
    from TopWindow import TopWindow
  File "TopWindow.py", line 4, in <module>
    import MainWindow
  File "MainWindow.py", line 4, in <module>
    from TopWindow import TopWindow
ImportError: cannot import name TopWindow

I believe the problem is that both classes import each other. However I am uncertain how to fix this. Any ideas?

Mainwindow.py:

from TopWindow import TopWindow

class MainWindow:
    def __init__(self, mainWindow):
        self.value = ''
        self.gui(mainWindow)
    def gui(self, mainWindow):
        top = TopWindow()
        button = tk.Button(optionFrame, text="Open TopWindow", command=top.import)
        ...

    def addtolistbox(self):
        ....


mainWindow = tk.Tk()
run = MainWindow(mainWindow)
mainWindow.mainloop()

TopWindow.py

import MainWindow
class TopWindow:
    def import(self):
        ....
        MainWindow.MainWindow().addtolistbox()
2
  • def import(self): i think it's your problem. it should be __init__() Commented Dec 17, 2013 at 14:22
  • Is the naming choice meant to be funny? Commented Dec 17, 2013 at 14:33

1 Answer 1

1

You created a circular import; when importing MainWindow from TopWindow, the TopWindow class is not yet defined, so the TopWindow module as seen by MainWindow doesn't have that attribute.

Postpone importing in the TopWindow class:

class TopWindow:
    def import(self):
        ....
        import MainWindow
        MainWindow.MainWindow().addtolistbox()

Next, you are creating a new copy of the MainWindow object each time; you probably want to access the singleton object created in MainWindow.run instead:

class TopWindow:
    def import(self):
        ....
        import MainWindow
        MainWindow.run.addtolistbox()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! That indeed fixes the import error. However, now I have a different problem. By calling MainWindow.MainWindow().addtolistbox(), I create a new Mainwindow instead of just updating the list on the existing window. I don't want a new MainWindow created, but a function called within the MainWindow to update the existing one. How would I do that?
In addition, the new MainWindow isn't in the mainloop anymore. I'm getting a TypeError: __init__() takes exactly 2 arguments (1 given)
@user3111516: Perhaps you wanted to use MainWindow.run.addtolistbox() instead? You already created your main window object in that module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.