This program gives me a circular import error but still works

YamiAtem

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

baisbdhfug

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails: Faye works for me, but still gives some JS error in the console for some unknown reason, Should I be worried?

From Dev

Getting 'Type Mismatch' Error but program still works

From Dev

Small sample program gives me an error

From Dev

rhc setup gives error `no such file dl/import` still

From Dev

Guzzle post gives me error 500, get works fine

From Dev

C Program works for me but shows runtime error online

From Dev

MySQL 'AND' gives me error

From Dev

It gives me an error at GPS

From Dev

Python3.4: Opening file with mode 'w' still gives me FileNotFound error

From Dev

Python IDLE won't import script (gives error) but script works fine from textwrangler

From Dev

Python: Using pandas to import csv. Trying to plot a column but gives me an error saying "no numerical data to plot"

From Dev

Running Java Program in another Program gives error

From Dev

Running Java Program in another Program gives error

From Dev

my program runs but it gives me no output

From Dev

Animate UITableViewCell gives me error

From Dev

Sorting array gives me error

From Dev

php header gives me the error

From Dev

.getDownloadUrl() gives me error - Android

From Dev

using "this" in a fragment gives me an error

From Dev

Sorting array gives me error

From Dev

Animate UITableViewCell gives me error

From Dev

Firefox gives me dbus error

From Dev

Python variable is declared and still gives error

From Dev

Python import error. Circular imports

From Dev

Trying to trace a circular import error in Django

From Dev

Python import error. Circular imports

From Dev

Valgrind: libmysql example program gives 'still reachable:' leak

From Dev

R shiny - shiny app works well locally and on others shiny server but it does not work on my server. It gives me NO error message

From Dev

Opening file throws error but still works

Related Related

  1. 1

    Rails: Faye works for me, but still gives some JS error in the console for some unknown reason, Should I be worried?

  2. 2

    Getting 'Type Mismatch' Error but program still works

  3. 3

    Small sample program gives me an error

  4. 4

    rhc setup gives error `no such file dl/import` still

  5. 5

    Guzzle post gives me error 500, get works fine

  6. 6

    C Program works for me but shows runtime error online

  7. 7

    MySQL 'AND' gives me error

  8. 8

    It gives me an error at GPS

  9. 9

    Python3.4: Opening file with mode 'w' still gives me FileNotFound error

  10. 10

    Python IDLE won't import script (gives error) but script works fine from textwrangler

  11. 11

    Python: Using pandas to import csv. Trying to plot a column but gives me an error saying "no numerical data to plot"

  12. 12

    Running Java Program in another Program gives error

  13. 13

    Running Java Program in another Program gives error

  14. 14

    my program runs but it gives me no output

  15. 15

    Animate UITableViewCell gives me error

  16. 16

    Sorting array gives me error

  17. 17

    php header gives me the error

  18. 18

    .getDownloadUrl() gives me error - Android

  19. 19

    using "this" in a fragment gives me an error

  20. 20

    Sorting array gives me error

  21. 21

    Animate UITableViewCell gives me error

  22. 22

    Firefox gives me dbus error

  23. 23

    Python variable is declared and still gives error

  24. 24

    Python import error. Circular imports

  25. 25

    Trying to trace a circular import error in Django

  26. 26

    Python import error. Circular imports

  27. 27

    Valgrind: libmysql example program gives 'still reachable:' leak

  28. 28

    R shiny - shiny app works well locally and on others shiny server but it does not work on my server. It gives me NO error message

  29. 29

    Opening file throws error but still works

HotTag

Archive