代码之家  ›  专栏  ›  技术社区  ›  Justin

与范围相关的变量有问题

  •  0
  • Justin  · 技术社区  · 8 年前

    你好,各位程序员,我正在用Python开发一个简单的界面应用程序,它可以在Access数据库中轻松直观地输入库存表单。

    我目前的功能如下:

    def spawnerror(self, errormsg):
        self.running = False
        content = Button(text=errormsg)
        popup = Popup(title='ERROR!', content=content, auto_dismiss=False)
        content.bind(on_press=popup.dismiss)
        popup.open()
    

    我已经完成了适当的错误处理,应用程序按预期使用这个函数。例如,如果有人没有输入一个必需的字段,它将调用此函数并生成一个带有错误的错误页面,并通知用户。

    我遇到的问题是,它需要设置类变量 正在运行 设置为false,因为在主函数“submit”的末尾,它会检查该值,如果self.running==false,则需要跳过Access数据库中数据项的执行。

    为什么此函数不将running类变量设置为false?

    1 回复  |  直到 8 年前
        1
  •  1
  •   ikolim    8 年前

    解决方案-使用app.get_running_app()。

    在该示例中,类属性 running 被定义为booleanProperty。在 spanError()中,它使用 app.get_running_app()来获取app class的实例,然后访问变量, running

    注意

    如果运行 , SpawError(), Function and Submit(), Function are in different classes then work out the relationship of the classes and pass a direct reference between them.

    示例

    主要.py

    从kivy.app导入app 从kivy.uix.popup导入弹出菜单 从kivy.uix.button导入按钮 从kivy.uix.boxlayout导入boxlayout 从kivy.properties导入booleanProperty,objectProperty 类rootwidget(boxlayout): 实例=ObjectProperty(无) 定义初始化(self,**kwargs): 超级(rootwidget,self)。初始化(*kwargs) self.instance=app.get_running_app()。 self.spawError('测试') def产卵错误(self,errormsg): self.instance.running=false 内容=按钮(文本=错误消息) popup=popup(title='错误!',content=content,auto_disclish=false) content.bind(on_press=popup.disclose) 弹出窗口。打开() 类testapp(app): 运行=布尔属性(真) 定义构建(自身): 打印(“\n生成:”) self.display_属性() 返回rootwidget()。 def on ou stop(自我): 打印(“\n停止:”) self.display_属性() def显示属性(self): 打印(“\tapp.running=”,self.running) 如果“名称”, testapp().run()。

    输出

    珀蒂。在spawnerror()函数,它使用App.get_running_app()函数获取app类的实例,然后访问变量,正在运行.

    注意

    如果正在运行,请生成错误()功能和提交()函数在不同的类中,然后计算出类之间的关系,并在它们之间传递一个直接引用。

    例子

    主.py

    from kivy.app import App
    from kivy.uix.popup import Popup
    from kivy.uix.button import Button
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import BooleanProperty, ObjectProperty
    
    
    class RootWidget(BoxLayout):
        instance = ObjectProperty(None)
    
        def __init__(self, **kwargs):
            super(RootWidget, self).__init__(**kwargs)
            self.instance = App.get_running_app()
            self.spawnerror('Testing')
    
        def spawnerror(self, errormsg):
            self.instance.running = False
            content = Button(text=errormsg)
            popup = Popup(title='ERROR!', content=content, auto_dismiss=False)
            content.bind(on_press=popup.dismiss)
            popup.open()
    
    
    class TestApp(App):
        running = BooleanProperty(True)
    
        def build(self):
            print("\nbuild:")
            self.display_attributes()
            return RootWidget()
    
        def on_stop(self):
            print("\non_stop:")
            self.display_attributes()
    
        def display_attributes(self):
            print("\tApp.running =", self.running)
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    输出

    Img01