【问题标题】:This program gives me a circular import error but still works这个程序给了我一个循环导入错误,但仍然有效
【发布时间】: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


    【解决方案1】:

    循环错误可能是因为当您导入文件时,全局范围内的所有代码都会被执行,并且该代码可能需要您最初运行的文件中的函数。

    我尝试运行您的代码,虽然它对我来说运行不正常,但我知道您想要实现的目标。您正在尝试实现选项卡功能,但每次切换到选项卡时都会打开一个新窗口。所以,我建议使用 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')
    
    

    现在您不需要为每个选项卡创建一个新文件,因此将所有代码保存在一个文件中,同时放置按钮之类的东西,而不是将它们放在根目录中,将它们直接放到选项卡中。

    例如我想在 Screen1 选项卡中添加一个按钮:

    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)
    

    总而言之,您必须进行的更改是添加我编写的第一段代码,将您的所有代码放入一个文件中,并将按钮和标题等内容的第一个参数从根替换为选项卡属于。

    如果有什么不清楚的可以随时问我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多