【发布时间】:2020-10-22 18:41:38
【问题描述】:
问题
我正在使用 Python3 和 Kivy2。无论如何,我希望弹出窗口在后台自动启动一个耗时的方法
描述 在满足条件时弹出的脚本调用中。
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语言部分是:
<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 中的按钮被禁用,因为我只想显示标签。以下是我检查过的一些类似问题,但没有找到解决方案。
- Displaying something in kivy while a process is running background
- Kivy: how to display a widget while waiting for another one to be displayed (both called from a same event)
- Kivy popup running in separate thread
- Pop up Kivy displaying while a process is running in background
- Open kivy popup before continuing
问题
有没有办法从 ActivityBox 中自动启动一个方法,而不将它绑定到按钮上?
更新 1
我尝试在 def popup(self, boxTitle, boxLabel): 中插入方法调用,但弹出窗口仅在方法终止后出现。
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
我尝试了线程但没有成功。弹出窗口和方法各自在一个单独的线程中。弹出窗口仅在方法终止后出现。
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()
【问题讨论】:
标签: python methods popup kivy background-process