Dav*_*d K 7 python events user-interface kivy
我正在为我开发的cmd行实用程序编写一个Kivy UI.一切正常,但有些过程可能需要几秒钟到几分钟才能完成,我想向用户提供一些过程正在运行的指示.理想情况下,这将是旋转轮或装载杆或其他形式,但即使我可以更新我的显示器以向用户显示进程正在运行,它将比我现在更好.
目前,用户按下主UI中的按钮.这会弹出一个弹出窗口,用户可以验证一些关键信息,如果他们对这些选项感到满意,他们会按下"运行"按钮.我尝试打开一个新的弹出窗口告诉他们进程正在运行,但是因为显示在进程完成之前不会更新,所以这不起作用.
我失去了编码经验,但主要是在数学和工程方面,所以我对UI的设计非常陌生,并且必须处理事件和线程.一个简单的自包含的例子将非常感激.
我最近解决了你所描述的问题: display doesn't update until the process finishes
这是一个完整的例子,我在#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()
Run Code Online (Sandbox Code Playgroud)
我的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: ''
Run Code Online (Sandbox Code Playgroud)
如果运行此示例,则可以单击Click Me按钮,该按钮应以模式/弹出窗口的形式打开"进度条".此弹出窗口将保持打开状态5秒钟而不会阻止主窗口.5秒后,弹出窗口将自动被解除.
| 归档时间: |
|
| 查看次数: |
7304 次 |
| 最近记录: |