問題描述
我正在為我開發的命令行實用程序編寫 Kivy UI.一切正常,但有些進程可能需要幾秒鐘到幾分鐘的時間來處理,我想向用戶提供一些指示,表明該進程正在運行.理想情況下,這將是一個旋轉輪或加載欄或其他東西的形式,但即使我可以更新我的顯示以向用戶顯示一個進程正在運行,它也會比我現在擁有的更好.
I am writing a Kivy UI for cmd line utility I have developed. Everything works fine, but some of the processes can take from a few seconds to a few minutes to process and I would like to provide some indication to the user that the process is running. Ideally, this would be in the form of a spinning wheel or loading bar or something, but even if I could update my display to show the user that a process is running, it would be better than what I have now.
目前,用戶在主 UI 中按下按鈕.這會彈出一個彈出窗口,向用戶驗證一些關鍵信息,如果他們對這些選項感到滿意,他們會按下運行"按鈕.我已經嘗試打開一個新的彈出窗口來告訴他們進程正在運行,但是因為在進程完成之前顯示不會更新,所以這不起作用.
Currently, the user presses a button in the main UI. This brings up a popup that verifies some key information with the user, and if they are happy with those options, they press a 'run' button. I have tried opening a new popup to tell them that the process is running, but because the display doesn't update until the process finishes, this doesn't work.
我有很多編碼經驗,但主要是在數學和工程方面,所以我對 UI 設計和處理事件和線程非常陌生.一個簡單的獨立示例將不勝感激.
I have a lot of coding experience, but mostly in the context of math and engineering, so I am very new to the designing of UIs and having to handle events and threads. A simple self-contained example would be greatly appreciated.
推薦答案
我最近正在解決你描述的問題:顯示在進程完成之前不會更新
I was recently tackling the problem you described: display doesn't update until the process finishes
這是我在#Kivy IRC 頻道中@andy_s 的幫助下完成的一個完整示例:
Here is a complete example that I got working with the help of @andy_s in the #Kivy IRC channel:
我的 main.py:
My 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()
我的例子.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: ''
如果你運行這個例子,你可以點擊Click Me
按鈕,它應該會打開一個模態/彈窗形式的進度條".此彈出窗口將保持打開 5 秒鐘而不會阻塞主窗口.5秒后,彈窗會自動消失.
If you run this example, you can click the Click Me
button, which should open up a "progress bar" in the form of a modal/pop-up. This pop up will remain open for 5 seconds without blocking the main window. After 5 seconds, the pop up will automatically be dismissed.
這篇關于在 Kivy 中構建簡單的進度條或加載動畫?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!