我正在为我开发的cmd行实用程序编写一个Kivy UI.一切正常,但有些过程可能需要几秒钟到几分钟才能完成,我想向用户提供一些过程正在运行的指示.理想情况下,这将采用旋转轮或装载杆等形式,但即使我可以更新我的显示器以向用户显示进程正在运行,它也会比我现在拥有的更好.
目前,用户按下主UI中的按钮.这会弹出一个弹出窗口,用户可以验证一些关键信息,如果他们对这些选项感到满意,他们会按下“运行”按钮.我尝试打开一个新的弹出窗口告诉他们进程正在运行,但是因为显示在进程完成之前不会更新,所以这不起作用.
我有很多编码经验,但主要是在数学和工程方面,所以我对UI的设计和处理事件和线程都很新.我们将非常感谢一个简单的独立示例. 解决方法: 我最近解决了您描述的问题:显示在进程完成之前不会更新
这是一个完整的例子,我在#Kivy IRC频道的@andy_s的帮助下工作:
我的main.py:
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.clock import Clock
import time, threading
class PopupBox(Popup):
pop_up_text = ObjectProperty()
def update_pop_up_text(self, p_message):
self.pop_up_text.text = p_message
class ExampleApp(App):
def show_popup(self):
self.pop_up = Factory.PopupBox()
self.pop_up.update_pop_up_text('Running some task...')
self.pop_up.open()
def process_button_click(self):
# Open the pop up
self.show_popup()
# Call some method that may take a while to run.
# I'm using a thread to simulate this
mythread = threading.Thread(target=self.something_that_takes_5_seconds_to_run)
mythread.start()
def something_that_takes_5_seconds_to_run(self):
thistime = time.time()
while thistime 5 > time.time(): # 5 seconds
time.sleep(1)
# Once the long running task is done, close the pop up.
self.pop_up.dismiss()
if __name__ == "__main__":
ExampleApp().run()
我的example.kv:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
Button:
height: 40
width: 100
size_hint: (None, None)
text: 'Click Me'
on_press: app.process_button_click()
<PopupBox>:
pop_up_text: _pop_up_text
size_hint: .5, .5
auto_dismiss: True
title: 'Status'
BoxLayout:
orientation: "vertical"
Label:
id: _pop_up_text
text: ''
如果您运行此示例,则可以单击“单击我”按钮,该按钮应以模式/弹出窗口的形式打开“进度条”.此弹出窗口将保持打开状态5秒钟而不会阻止主窗口. 5秒后,弹出窗口将自动被解除. 来源:https://www./content-1-478101.html
|