1

I want to create a screen manager in python. A menu with options for screens to go, and on each screen an option to go back to the menu. If I put the classes in the same file I won't have a problem, but if I try to modularize and distribute the classes in files, I can't import the 'main()' function in FirstScreen, for example. Each screen is in a different class and in a different file, when I try to go back to the menu the circle error will occur. What is the best way to solve it? Here is the code for each file:

Main

from tkinter import *
from primeira import *

class main():
    def __init__(self):
        self.master = Tk()

        self.master.title('main window')
        self.master.geometry('480x240')
        self.master.configure(borderwidth=4, background='white')

        self.button = Button(self.master, text='window one', command= lambda: self.evento())
        self.button.pack(side='left', fill='x')

        self.master.mainloop()

    def evento(self):
        self.master.destroy()
        FirstWindow()
main()

First window (in another archive)

from tkinter import *
from main import main

class FirstWindow():
    def __init__(self, master=None):
        master = Tk()
        self.master = master

        self.master.title('window one')
        self.master.configure(background='green')
        self.master.geometry('480x240')

        self.button = Button(master, text='menu', command= lambda: self.goMain())
        self.button.pack(side='left', fill='x', expand=True)
        master.mainloop()

    def goMain(self):
        self.master.destroy()
        main()
martineau
  • 119,623
  • 25
  • 170
  • 301
Maria
  • 11
  • 1
  • I suggest you take a look at the answer to [Using buttons in Tkinter to navigate to different pages of the application?](https://stackoverflow.com/questions/14817210/using-buttons-in-tkinter-to-navigate-to-different-pages-of-the-application) which is similar. – martineau Oct 14 '21 at 22:44
  • Hey martineau, thank you! I didnt had look this. In this, how i can put a button in the page that return a string to the main? – Maria Oct 14 '21 at 23:25
  • I assume you meant you **did** have a look at it. You can't return values of any kind from a button no matter where it is. What you can do is create tkinter [variables](https://web.archive.org/web/20201031100214id_/https://effbot.org/tkinterbook/variable.htm) of the appropriate type and make them attributes of the `MainView` instance which is passed as the first argument—`args[0]`—to each of the `Page` classes. Those classes can then access them (read and modify their values) through this argument if its saved as a `Page` instance attribute. This means the command for a button could do it too. – martineau Oct 14 '21 at 23:45
  • Here's a similar question (with a similar answer) about having multiple windows you should also look at: [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) Here's one about [How to access variables from different classes in tkinter?](https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter) that addresses your follow-up question in the comment above. – martineau Oct 14 '21 at 23:55
  • Hey matineau, thank you again! You are a genius. You helped me a lot! – Maria Oct 15 '21 at 23:06
  • It's good to hear you found my comments helpful. – martineau Oct 16 '21 at 00:55

0 Answers0