python - 为什么一次在两个文件/模块上使用导入会给我错误,但当我只对一个文件/模块执行时却不会?
问题描述
请帮忙!我是 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)
为了便于查看,我只包括了相关的行。
解决方案
推荐阅读
- php - 在这个错误中该怎么办?致命错误 go4shop
- c++ - log4cplus:动态加载配置脚本,这个问题怎么解决?
- python - 使用 horovod 和 slurm 扩展 keras 训练
- objective-c - 无法从 Objective-C++ 代码调用 swift 函数
- .htaccess - 在 htaccess 中删除部分 URL 的脚本
- javascript - 如何在 setInterval 中异步运行两个函数?
- angularjs - 可以学习最新版本的Angular js来理解和升级旧版本写的代码吗?
- php - PHP Instagram 抓取加载更多按钮问题
- python - 使用rest api访问linkedin SSI?
- angular - 在 Angular 路由器中动态传递数据