Search code examples
pythonappjar

Modualrize Python program with appJar GUI


How do you modularize an appJar app? When I try to use the example code and move the press() function into an own file, this functions doesn't know the app variable. I guess it's best if you look at the Image below - if not I'll update the question!

Example


Solution

  • As for me it is not good idea to put press() in separated file. I would rather keep press() in current file and it could use function from other file - but this file should get all values as arguments

    modules/methods.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])
    

    Or event other function should get only values from widgets

    modules/methods.py

    def display(username, password):
        print("User:", username, "Pass:", password)
    

    main.py

    def press():
        methods.display(app.entry("Username"), app.entry("Password"))
    

    If you really want press() in other file then it should get app as argument

    modules/methods.py

    def press(app):
        print("User:", app.entry("Username"), "Pass:", app.entry("Password"))
    

    but then you have to use lambda:methods.press(app) to assign to button function with argument.

    main.py

    app.buttons(["Submit", "Cancel"], [lambda:methods.press(app), app.stop])