"应用"从单独的模块使用函数时,从appJar未定义

时间:2018-05-02 01:28:36

标签: python user-interface

我的代码包含程序的GUI和当前两个独立的函数。为了保持我的main.py文件有点整洁,我想将我的功能分成其他文件。但是,当我这样做并将所述文件导入main.py时,当代码运行并且我尝试使用与导入函数相关的事件时,我得到一个错误,其中显示" app"未定义。问题是我不确定如何在这种情况下定义它以便它可以工作。

问题是:如何在使用其他文件中的函数时定义应用程序?

完整错误:回溯(最近一次调用最后一次):   文件" C:\ Users \ Jason Smit \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ tkinter__init __。py",第1702行,调用     return self.func(* args)   文件" C:\ Users \ Jason Smit \ PycharmProjects \ Project-X \ venv \ lib \ site-packages \ appJar \ appjar.py",第3494行,     return lambda * args:funcName(param)   文件" C:\ Users \ Jason Smit \ PycharmProjects \ Project-X \ menu_events.py",第10行,在工具栏中     如果是app.exitFullscreen(): NameError:name' app'未定义

from appJar import gui

# Global Constants:

FILE_MENUS = ["Open", "Save", "Save as...", "Export", "Close"]
HELP_MENUS = ["Help", "Getting Started", "Settings", "Submit Feedback", "About"]


def login(btn):
    if btn == "Unlock":
        app.infoBox("Success", "Access granted\n Welcome User")
        app.hideSubWindow("Sub1")
        app.show()
    return

# Holds event handlers for toolbar


def toolbar(btn):
    print(btn)
    if btn == "LOGOUT":
        app.hide()
        app.showSubWindow("Sub1")
    elif btn == "FULL-SCREEN":
        if app.exitFullscreen():
            app.setToolbarIcon("FULL-SCREEN", "FULL-SCREEN")
        else:
            app.setSize("fullscreen")
            app.setToolbarIcon("FULL-SCREEN", "FULL-SCREEN-EXIT")


###################
# GUI starts here # 
###################


with gui("Project-X", "1700x1000") as app:
    app.setBg("white")
    app.setIcon("app_icon.ico")  # App icon

    # Subwindow for user login
    with app.subWindow("Sub1", "Login",):
        app.setBg("white")

        # Empty left container (To help get login in center.)
        with app.frame("LEFT", row=0, column=0, sticky='NEW', stretch='COLUMN'):
            """"""
        # Login container/gui

        with app.frame("CenterTop", row=0, column=1, sticky='NEW', stretch='column'):
            app.setBg("white")
            with app.labelFrame("Login"):
                app.setBg("white")
                app.setSticky("ew")
                # Login widget setup
                app.addLabel("Usr", "Name", 0, 0)
                app.addEntry("Name", 0, 1)
                app.addLabel("Pwd", "Password", 1, 0)
                app.addSecretEntry("Password", 1, 1)
                app.addNamedButton("Submit", "Unlock", login, 2, 0, 2)

        # Empty right container (To help get login in center.)

        with app.frame("RIGHT", row=0, column=2,  sticky='NEW', stretch='COLUMN'):
            """"""
    # Top-level menu for main window

    app.addMenuList("File", FILE_MENUS, toolbar)
    app.addMenuList("Help", HELP_MENUS, toolbar)

    # Main navigation Toolbar
    app.addToolbar(["LOGOUT", "FULL-SCREEN"], toolbar, findIcon=True)

    # Tabbed page for displaying info for each category
    with app.tabbedFrame("Item Inventory"):
        app.startTab("Category_1")
        app.addLabel("l1", "Test for category tab 1")
        app.stopTab()

        app.startTab("Category_2")
        app.addLabel("l2", "Test for category tab 2")
        app.stopTab()

    # Start application from login sub window
    app.go(startWindow="Sub1")

1 个答案:

答案 0 :(得分:0)

logintoolbar函数引用行app范围之外的with gui("Project-X", "1700x1000") as app:对象。因此,函数需要在with行之后,缩进一次。

或者,您可以在调用它们时将app对象传递给这些函数,但这似乎很迂回(我没有尝试过类似的东西) - 因为函数非常关于 gui,它们应该与gui的其余部分保持在同一个文件中。