微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在完成按钮执行的功能之前,让Kivy更新PopUp

如何解决在完成按钮执行的功能之前,让Kivy更新PopUp

关于问题: 在sutPopUp.create()中,我试图让弹出窗口进行更新(删除了两个按钮并更改了“隐藏”标签) 。我已经尝试过将更新函数调用 Clock.schedule_once(self.update_txt,-1)都进行了线程连接,但是都没有一个起作用。他们似乎都在等待 run_local_command,这只是一个运行本地命令的阻塞函数。以下是python代码

class sutPopUp(Popup):
    pop_float = ObjectProperty(None)
    sutPopUp_create = ObjectProperty(None)
    sutPopUp_cancel = ObjectProperty(None)
    sut_wait_text = ObjectProperty(None)
    sutPopUp_input = ObjectProperty(None)

    def __init__(self,my_widget,**kwargs):
        super(sutPopUp,self).__init__(**kwargs)
        self.title = "Test Station Setup"
        self.size_hint = (None,None)
        self.size = (400,200)

    def create(self,*args):
        self.quick_change_thread = threading.Thread(target=self.update_txt)
        self.quick_change_thread.start()
        time.sleep(1)

        sut_name = self.sutPopUp_input.text
        create_cmd = "python project.py -c " + sut_name
        create_handle = run_local_command(create_cmd,True,"C:\Project")

        wm.current = "blank"
        wm.remove_widget(screens[2])
        screens[2] = Dashboard(name="dashboard_screen")
        wm.add_widget(screens[2])
        wm.current = "dashboard_screen"

        self.dismiss()
        self.quick_change_thread.join()

    def update_txt(self):
        self.sutPopUp_create.disabled = True
        self.sutPopUp_cancel.disabled = True
        self.pop_float.remove_widget(self.sutPopUp_create)
        self.pop_float.remove_widget(self.sutPopUp_cancel)
        self.sut_wait_text.text = "Creating Test Station ..."

这是kv:

<sutPopUp@Popup>
    pop_float:pop_float
    sutPopUp_create:sutPopUp_create
    sutPopUp_cancel:sutPopUp_cancel
    sut_wait_text:sut_wait_text
    sutPopUp_input:sutPopUp_input

    FloatLayout:
        id: pop_float
        size: root.height,root.width
        Label:
            text: "Enter Test Station Name:"
            pos_hint:{"x":0.1,"top":0.9}
            size_hint: 0.25,0.2
        TextInput:
            id: sutPopUp_input
            multiline: False
            pos_hint:{"x":0,"top":0.7}
            size_hint: 1,0.2
        Label:
            id: sut_wait_text
            text: ""
            pos_hint:{"x":0.4,"top":0.3}
            size_hint: 0.25,0.2
        Button:
            id: sutPopUp_create
            text: "Create"
            pos_hint:{"x":0.1,"top":0.3}
            size_hint: 0.3,0.2
            on_release: root.create()

        Button:
            id: sutPopUp_cancel
            text: "Cancel"
            pos_hint:{"x":0.6,0.2
            on_release: root.dismiss()

解决方法

我建议将create()方法分为三种方法。由于create()方法是由Button按下启动的,因此它将在主线程上运行,因此只需直接调用update_text()方法即可。然后使用另一个线程来运行create_cmd。然后,该新线程使用Clock.schedule_once()在主线程上运行原始create()方法(新finish_create()方法)中的其余代码。

def create(self,*args):
    self.update_text()  # this needs to run on the main thread
    # time.sleep(1)  # ??? this will freeze your app for 1 second

    Thread(target=self.do_create).start()

def do_create(self):
    sut_name = self.sutPopUp_input.text
    create_cmd = "python project.py -c " + sut_name
    create_handle = run_local_command(create_cmd,True,"C:\Project")

    # after above command completes,run the rest of the former create() back on the main thread
    Clock.schedule_once(self.finish_create)

def finish_create(self,dt):
    wm.current = "blank"
    wm.remove_widget(screens[2])
    screens[2] = Dashboard(name="dashboard_screen")
    wm.add_widget(screens[2])
    wm.current = "dashboard_screen"

    self.dismiss()

无论您做什么,当有一个函数保留主线程时,GUI中的任何内容都不会更新。因此,这种方法可以最大程度地减少在主线程上花费的时间。该代码未经测试,因此可能存在错误。但是我相信这种方法应该可以实现您想要的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。