问题是因为你确实有一个循环import
。模块One
导入模块Two
导入模块One
...等等。然而,@acw1668 建议的简单修复不足以修复问题,因为Two
模块引用不仅仅是Window
的属性One
模块。我的解决方案传递了模块中的内容One
模块中的函数Two
需要作为参数(所以Two
模块不需要import
来访问它们)。
问题中讨论了 tkinter 代码的另一个问题Tkinter:AttributeError:NoneType 对象没有属性 https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name,我建议你阅读。
以下是对两个模块进行的更改,以解决所有这些问题。
One.py
:
from tkinter import *
import Two
Window = Tk()
Window.title("Main Window")
Value_a = Entry(Window, width=15)
Value_a.grid(row=1, column=0)
Value_b = Entry(Window, width=15)
Value_b.grid(row=2, column=0)
my_button = Button(Window, text="Test",
command=lambda: Two.sum(Window, Value_a, Value_b))
my_button.grid(row=3, column=0)
Window.mainloop()
Two.py
:
from tkinter import *
def sum(Window, Value_a, Value_b):
newWindow = Toplevel(Window)
newWindow.title("Sum")
a = int(Value_a.get())
b = int(Value_b.get())
c = a+b
Label(newWindow, text= str(c)).grid(row=1, column=0)