Kivy-从弹出窗口自动启动方法 [英] Kivy - automatically start a method from popup

查看:89
本文介绍了Kivy-从弹出窗口自动启动方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我正在使用Python3和Kivy2.无论我如何,弹出窗口都会在后台自动启动耗时的方法

I am using Python3 and Kivy2. Regardless I would like a popup to automatically start a time consuming method in the background

说明 在脚本中,在满足条件时调用弹出窗口.

DESCRIPTION In the script call for the popup when a condition is met.

if  self.header == "loadbag":
    messageTitle = "machine status"
    messageLabel = "work in progress"
    self.popup(messageTitle, messageLabel)

弹出方法的结构为:

def popup(self, boxTitle, boxLabel):    # popup for showing the activity
    self.MessageButtonCancel = "ANNULLA"
    self.MessageBoxTitle = boxTitle
    self.MessageBoxLabel = boxLabel
    self.popup = ActivityBox(self)
    self.popup.open()

ActivityBox的Builder.load_string中的kv语言部分是:

The kv language part in the Builder.load_string for the ActivityBox is:

<ActivityBox>:
    size_hint: 1, .7
    auto_dismiss: False
    title: app.MessageBoxTitle       
    title_align: "center"
    title_size: 30

    BoxLayout:
        orientation: "vertical"
        Label:
            font_size: '30sp'
            text: app.MessageBoxLabel
        BoxLayout:
            orientation: "horizontal"
            spacing: 10
            size_hint: 1, .5
            # both buttons are not necessary
            # the popup just shows a message
            # Button:
                # font_size: 50
                # background_color: 0,204,0,1
                # text: app.MessageButtonConfirm  # "CONFIRM"
                # on_press:
                    # self.disabled = True
                    # self.background_color = 0,255,0,1
                    # app.do()
            # Button:
                # font_size: 50
                # background_color: 204,0,0,1
                # text: app.MessageButtonCancel  # "CANCEL"
                # on_press:
                    # self.background_color = 255,0,0,1
                    # app.cancel()
                    # root.dismiss()

如您所见,ActivityBox中的按钮被禁用,因为我只希望标签出现.以下是我在没有找到解决方案的情况下检查过的一些类似问题.

As you can see, the buttons in the ActivityBox are disabled as I only want the label to appear. Below are some similar questions I have checked without finding a solution.

  1. ​​在进程运行时以kivy显示内容背景
  2. Kivy:如何在等待另一个小部件显示时显示小部件(都是从同一事件调用的)
  3. 在单独的线程中运行的基辅弹出窗口
  4. ​​在处理过程中弹出Kivy显示在后台运行
  5. 在继续操作之前先打开kivy弹出框
  1. Displaying something in kivy while a process is running background
  2. Kivy: how to display a widget while waiting for another one to be displayed (both called from a same event)
  3. Kivy popup running in separate thread
  4. Pop up Kivy displaying while a process is running in background
  5. Open kivy popup before continuing

问题

是否有一种方法可以从ActivityBox内部自动启动方法,而无需将其绑定到按钮上?

Is there a way to start a method automatically from within the ActivityBox, without binding it to the buttons?

更新1

我试图在def popup(自身,boxTitle,boxLabel)中插入方法调用:但是弹出窗口仅在方法终止后才出现.

I tried to insert the method call in the def popup(self, boxTitle, boxLabel): but the popup appeared only after the method terminated.

def popup(self, boxTitle, boxLabel):    # popup for showing the activity
    self.MessageBoxTitle = boxTitle
    self.MessageBoxLabel = boxLabel
    self.popup = ActivityBox(self)
    self.time_consuming_method() # here goes the method to run
    self.popup.open()

更新2

我尝试了线程,但没有成功.弹出窗口和方法分别在单独的线程中.弹出窗口仅在方法终止后出现.

I tried with threading without success. The popup and the method each in a separate thread. The popup appeared only after the method terminated.

    def popup(self, boxTitle, boxLabel):    # popup for showing the activity
        self.MessageBoxTitle = boxTitle
        self.MessageBoxLabel = boxLabel
        self.popup = ActivityBox(self)
        self.popup.open()
    
    t1 = threading.Thread(target = popup, args = (boxTitle, boxLabel))
    t2 = threading.Thread(target = time_consuming_method)
    
    t1.start()
    t2.start()
    
    t1.join()
    t2.join()

推荐答案

使用Threadjoin()方法将导致等待,直到Thread完成为止,这几乎等同于不使用threading.

Using the join() method of a Thread causes a wait until that Thread has completed, which is almost equivalent to not using threading.

尝试消除线程t1,仅调用popup,而无需使用新线程.然后启动线程t2,但取消t2.join()调用.

Try eliminating thread t1, and just call popup without using a new thread for that. Then start thread t2, but eliminate the t2.join() call.

这篇关于Kivy-从弹出窗口自动启动方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆