So I have this GUI ith Tkinter that switch screens when I click on a button and the new screen has a back button to go back to the main screen. And if you close the other screen it takes you back to the main one again and if you close the main screen the app closes as it should. However, when switching for the main screen to another screen it works perfectly nothing wrong bt it give this error anyways:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "C:\Users\User\Desktop\Coding\Python\GmailSenderApp\GmailSenderApp.py", line 30, in <lambda>
button = tk.Button(root, text="Send Gmail without Attachments", font=('Courier', 14), command=lambda: screen1())
File "C:\Users\User\Desktop\Coding\Python\GmailSenderApp\GmailSenderApp.py", line 7, in screen1
Screen1.root.mainloop()
AttributeError: partially initialized module 'Screen1' has no attribute 'root' (most likely due to a circular import)
Here is the code for the main screen and second screen:
Main:
import tkinter as tk
import Screen1
def screen1():
root.destroy()
Screen1.root.mainloop()
HEIGHT = 500
WIDTH = 600
root = tk.Tk()
root.resizable(False, False)
root.title('Gmail Sender App')
icon = tk.PhotoImage(file='icon.png')
root.iconphoto(False, icon)
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_label = tk.Label(root, bg='#ffbe80')
background_label.place(relwidth=1, relheight=1)
title_label = tk.Label(root, bg='#80c1ff', text="Gmail Sender App", font=('Courier', 14))
title_label.place(width=root.winfo_screenwidth(), height=40, y=0, x=-375)
button = tk.Button(root, text="Send Gmail without Attachments", font=('Courier', 14), command=lambda: screen1())
button.place(width=400, height=40, x=root.winfo_width() / 2, y=root.winfo_height() / 2)
button2 = tk.Button(root, text="Send Gmail with Attachments", font=('Courier', 14))
button2.place(width=400, height=40, x=root.winfo_width() / 2, y=root.winfo_height() / 1.25)
root.mainloop()
Second Screen:
import tkinter as tk
import GmailSenderApp
def back():
root.destroy()
GmailSenderApp.root.mainloop()
HEIGHT = 500
WIDTH = 600
root = tk.Tk()
root.resizable(False, False)
root.title('Gmail Sender App')
icon = tk.PhotoImage(file='icon.png')
root.iconphoto(False, icon)
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_label = tk.Label(root, bg='#ffbe80')
background_label.place(relwidth=1, relheight=1)
title_label = tk.Label(root, bg='#80c1ff', text="Gmail Sender App", font=('Courier', 14))
title_label.place(width=root.winfo_screenwidth(), height=40, y=0, x=-375)
button = tk.Button(root, text="Back", font=('Courier', 14), command=lambda: back())
button.place(width=400, height=40, x=root.winfo_width() / 2, y=root.winfo_height() / 2)
root.mainloop()
So what am I doing wrong here?
also NOTE: that when clicking on the button to go to the screen again crashes the app
BTW Thanks in Advance
The circular error is probably caused because when you import a file all of the code that are in the global scope get executed and that code probably required a function from the file you initially ran.
I tried running your code although it didn't run properly for me I got an idea of what you are trying to achieve. You are trying to implement a tab feature but are opening a new window every time a tab is switched to. So, I'd recommend using the tabs that is built in with tkinter
from tkinter import ttk
tabs = ttk.Notebook(root)
Screen1 = ttk.Frame(tabs) # defining a new tab
tabs.add(Screen1, text='Screen1') # adding the new tab to the root
GmailSenderApp = ttk.Frame(tabs)
tabs.add(GmailSenderApp, text='GmailSenderApp')
Now you wont need a new file for each tab so keep all of the code in one file and while placing things like buttons instead of placing them in root place them directly to a tab.
For example I want to add a button to the Screen1 tab:
button = tk.Button(Screen1, text="Back", font=('Courier', 14), command=lambda: back())
button.place(width=400, height=40, x=root.winfo_width() / 2, y=root.winfo_height() / 2)
To sum it up the changes you have to make is add the first section of code that I've written bring all your code to one file and replace the first parameter for things like button and title from root to the tab that the belong to.
If something's not clear feel free to ask me.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments