从另一个文件执行 Button 命令?

Executing a Button command from another file?

我已经开始在一个 GUI 系统上工作,我需要从一个文件导入一个函数,以便在按下按钮时在主文件中执行,但是每次我 运行 它我得到:

AttributeError: partially initialized module 'Two' has no attribute 'sum'
  (most likely due to a circular import)

程序应该输入两个值,Value_aValue_b,被调用的函数 sum() 应该将这两个值相加并在新的 window。这是我要导入的文件及其功能的示例 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()

当这是 运行 时,我最终得到了上述错误。

问题是因为您确实有一个通告 import。模块 One 导入模块 Two,模块 Two 导入模块 One...而不仅仅是 One 模块的 Window 属性。我的解决方案传递模块 One 中的内容,模块 Two 中的函数需要作为参数(因此 Two 模块不需要 import 它来访问它们) .

问题 Tkinter: AttributeError: NoneType object has no attribute 中讨论了您的 tkinter 代码的另一个问题,我建议您阅读该问题。

以下是对解决所有这些问题的两个模块的更改。

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)

(这是另一个答案,它与我最初在其他答案中的答案非常相似,然后再做一些我觉得更简单的事情。我将其作为单独的答案发布,因为它事实证明 idea of a module getting a reference to itself has been around for a very long time,这样做并不像我最初想象的那样奇怪或骇人听闻。)

问题是因为您确实有一个通告 import。模块 One 导入模块 Two,模块 Two 导入模块 One...而不仅仅是 One 模块的 Window 属性。我的解决方案将 whole One 模块作为参数传递给函数(因此 Two 模块不需要 import 它来获得访问权限到它的属性)。

问题 Tkinter: AttributeError: NoneType object has no attribute 中讨论了您的 tkinter 代码的另一个问题,我建议您阅读该问题。

以下是对解决所有这些问题的两个模块的更改。为了避免循环导入,Button 命令现在将调用模块作为参数传递给模块 Two 中的 sum() 函数。虽然做这样的事情 有点不寻常,但如果您考虑一下(并希望避免循环导入),实际上是非常合乎逻辑的。

One.py:

from tkinter import *
import Two

CURRENT_MODULE = __import__(__name__)

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(CURRENT_MODULE))
my_button.grid(row=3, column=0)

Window.mainloop()

Two.py:

from tkinter import *

def sum(One):
    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)