使用appJar GUI修改Python程序

2024-05-23 18:09:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如何模块化appJar应用程序?当我尝试使用example code并将press()函数移动到自己的文件中时,该函数不知道app变量。我想你最好看看下面的图片,如果没有,我会更新这个问题

Example


Tags: 文件函数app应用程序example图片code模块化
1条回答
网友
1楼 · 发布于 2024-05-23 18:09:20

对我来说,把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])

相关问题 更多 >