【发布时间】:2021-01-31 09:19:15
【问题描述】:
所以我有这个带有 Tkinter 的 GUI,当我单击一个按钮时,它会切换屏幕,并且新屏幕有一个返回按钮可以返回到主屏幕。如果您关闭另一个屏幕,它会将您再次带回主屏幕,如果您关闭主屏幕,则应用程序会正常关闭。但是,当从主屏幕切换到另一个屏幕时,它完全没有问题,但无论如何它都会给出这个错误:
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)
这里是主屏和副屏的代码:
主要:
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()
第二屏:
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()
那么我在这里做错了什么?
另请注意:当再次单击按钮转到屏幕时,应用程序会崩溃
顺便说一句,提前致谢
【问题讨论】:
标签: python python-3.x tkinter