我已经开始在一个 GUI 系统上工作,我需要从一个文件中导入一个函数,以便在按下按钮时在主文件中执行,但是每次运行它时,我都会得到:
AttributeError: partially initialized module 'Two' has no attribute 'sum'
(most likely due to a circular import)
程序应该输入两个值,Value_a
和Value_b
,被调用的函数应该将这两个值sum()
相加并在新窗口中输出结果。这是我要导入的文件及其功能的示例sum()
:
Two.py
:
from tkinter import * #Import the tkinter module
import One #This is the main file, One.py
def sum():
newWindow = Toplevel(One.Window)
newWindow.title("Sum")
a = int(One.Value_a.get())
b = int(One.Value_b.get())
c = a+b
Label(newWindow, text= str(c)).grid(row=1, column=0)
这是主文件的样子:
One.py
:
from tkinter import *
import Two
Window = Tk()
Window.title("Main Window")
Value_a = Entry(Window, width=15).grid(row=1, column=0)
Value_b = Entry(Window, width=15).grid(row=2, column=0)
my_button = Button(Window, text="Test", command=lambda: Two.sum).grid(row=3, column=0)
Window.mainloop()
当它运行时,我最终得到上述错误。