对我来说,将press() 放在单独的文件中并不是一个好主意。我宁愿将press() 保留在当前文件中,它可以使用其他文件中的函数 - 但这个文件应该将所有值作为参数
模块/方法.py
def display(app):
print("User:", app.entry("Username"), "Pass:", app.entry("Password"))
main.py
from appJar import gui
from modules import methods
def press():
methods.display(app)
with gui("Login Window", "400x200", bg='orange', font={'size':18}) as app:
app.label("Welcome to appJar", bg='blue', fg='orange')
app.entry("Username", label=True, focus=True)
app.entry("Password", label=True, secret=True)
app.buttons(["Submit", "Cancel"], [press, app.stop])
或者事件其他函数应该只从小部件中获取值
模块/方法.py
def display(username, password):
print("User:", username, "Pass:", password)
main.py
def press():
methods.display(app.entry("Username"), app.entry("Password"))
如果你真的想要 press() 在其他文件中,那么它应该得到 app 作为参数
模块/方法.py
def press(app):
print("User:", app.entry("Username"), "Pass:", app.entry("Password"))
但是你必须使用lambda:methods.press(app) 来分配给带有参数的按钮功能。
main.py
app.buttons(["Submit", "Cancel"], [lambda:methods.press(app), app.stop])