【问题标题】:AttributeError: partially initialized module 'first_window' has no attribute 'InitialWindow' (most likely due to a circular import)AttributeError:部分初始化的模块“first_window”没有属性“InitialWindow”(很可能是由于循环导入)
【发布时间】:2021-08-01 23:26:54
【问题描述】:

我正在 tkinter 上工作,我想在单击按钮后关闭当前窗口并打开一个新窗口,反之亦然,但出现循环导入错误。

ma​​in.py 文件:-

import requests
import first_window

sorted_data = {
    "title": searched_movie['Title'],
    "year": searched_movie['Year'],
    "rated": searched_movie['Rated'],
    "runtime": searched_movie['Runtime'],
    "genre": searched_movie['Genre'],
    "plot": searched_movie['Plot'],
    "language": searched_movie['Language'],
    "starring": searched_movie['Actors'],
    "imdb": searched_movie['imdbRating']
}

first_window.InitialWindow()

first_window.py 代码:-

from tkinter import Tk, Button, Label, Entry
import second_window


class InitialWindow:
    def __init__(self):
        self.window = Tk()
        self.window.title('Movie and Series Info')
        self.window.config(padx=50, pady=50, bg='black')

        # Button
        self.search_button = Button(text="Search", command=self.search)
        self.search_button.grid(row=4, column=0, sticky='ew', pady=12, columnspan=2)

        self.window.mainloop()

    def search(self):
        self.window.destroy()
        second_window.MainWindow()

second_window.py 代码:-

from tkinter import Tk, Button, Label
import main
import first_window


class MainWindow:
    def __init__(self):
        self.window = Tk()
        self.window.title('Movie and Series Info')
        self.window.config(padx=50, pady=50, bg='black')

        # Button
        self.home_button = Button(text="Home", command=self.home)
        self.home_button.grid(row=4, column=0, sticky='ew', pady=12, columnspan=2)

        self.window.mainloop()

    def home(self):
        self.window.destroy()
        first_window.InitialWindow()

当我从 second_window.py 中删除 import main 但我想使用 main.py 中的 sorted_data 时,它工作正常。我该怎么办,我尝试了很多解决方案,但都没有奏效。

【问题讨论】:

  • 您可以将sorted_data 单独放入一个文件中,这样您就不需要在second_window.py 中导入main。或者,您可以在创建窗口对象时将数据传递给它们。
  • @kemp 我正在使用 api,这就是为什么我将它放在 main.py 中并在窗口对象中传递数据,我想要在 second_window.py 中的数据,但我已经在 first_window 中创建了对象。 py.
  • 我不明白您关于使用导致限制的 API 的评论。要将其传递给对象,您可以将其传递给InitialWindow,然后将其传递给MainWindow

标签: python class tkinter


【解决方案1】:

您可以将first_window.InitialWindow() 移动到if __name__ == "__main__": 内部main.py

import requests
import first_window

sorted_data = {
    "title": searched_movie['Title'],
    "year": searched_movie['Year'],
    "rated": searched_movie['Rated'],
    "runtime": searched_movie['Runtime'],
    "genre": searched_movie['Genre'],
    "plot": searched_movie['Plot'],
    "language": searched_movie['Language'],
    "starring": searched_movie['Actors'],
    "imdb": searched_movie['imdbRating']
}

if __name__ == "__main__":
    first_window.InitialWindow()

那么first_window.InitialWindow()将不会被import main执行。

如果first_window 仅用于该行,您也可以将import first_window 移动到if 块中。

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2021-07-18
    • 2021-11-13
    • 2020-06-14
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 2022-07-13
    相关资源
    最近更新 更多