首页 > 解决方案 > 为什么一次在两个文件/模块上使用导入会给我错误,但当我只对一个文件/模块执行时却不会?

问题描述

请帮忙!我是 Python 新手,我很困惑为什么我的模块导入在一个方向上起作用,但在返回方向上也不起作用。本质上,我可以从 classes.py 导入到 mainProg.py,但不能从 mainProg.py 导入到 classes.py,这没有任何意义。我需要 classes.py 文件来调用 mainProg.py 中的“newTitle()”函数,但是一旦我添加一个导入来调用该函数,我就会收到以下错误:

Traceback (most recent call last):
  File "C:\Users\jonathan\PycharmProjects\test\mainProg.py", line 2, in <module>
    import classes
  File "C:\Users\jonathan\PycharmProjects\test\classes.py", line 3, in <module>
    from mainProg import newTitle
  File "C:\Users\jonathan\PycharmProjects\test\mainProg.py", line 19, in <module>
    menubar = classes.menuButtons(root)
AttributeError: module 'classes' has no attribute 'menuButtons'

Process finished with exit code 1

主程序.py

from tkinter import *
import classes

title = Label(pArea, text="*No File Loaded*")

def newTitle():
    title["text"] = classes.filename

classes.py(版本 1 无错误)

from tkinter import *
from tkinter.filedialog import askopenfilename

def newFile():
    Tk().withdraw() 
    filename = askopenfilename()
    print(filename)

classes.py(版本 2 错误)

from tkinter import *
from tkinter.filedialog import askopenfilename
from mainProg import newTitle

def newFile():
    Tk().withdraw() 
    filename = askopenfilename()
    newTitle()
    print(filename)

为了便于查看,我只包括了相关的行。

标签: pythonpython-3.xpython-importpython-module

解决方案


推荐阅读