2017-07-15 20 views
1

我正在嘗試編寫允許用戶清除數據庫的kivy應用程序,實質上是將應用程序重置爲默認值。我遇到的問題是,當用戶單擊按鈕清除並重置數據庫(設置屏幕上的「清除數據庫」按鈕)時,這是一個漫長的過程,我想要一個彈出框顯示一個旋轉的gif,直到過程結束。彈出框不會顯示,直到進程結束

問題是popup和cleardatabase進程似乎在同一時間發生,以便彈出窗口只閃爍,然後在cleardatabase進程結束時消失。

我已經探索過不同的帖子,並嘗試過其中的解決方案,但似乎沒有任何工作。

下面我已經包含了從我的實際代碼中導出的精簡代碼,它複製了問題,cleardatabase方法調用sleep(5)來模擬長進程。在最近的這次嘗試中,我已經在this post的解決方案之後對代碼進行了建模。也許問題是我正在使用ScreenManager?

這是所有在python2.7,順便說一句,和基維1.9.1我認爲。

在此先感謝您的幫助。

main.py

from kivy.uix.behaviors import ButtonBehavior 
from kivy.uix.popup import Popup 
from kivy.uix.image import Image 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.widget import Widget 
from kivy.uix.label import Label 
from kivy.uix.anchorlayout import AnchorLayout 
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty, \ 
     StringProperty 
from kivy.event import EventDispatcher 
import threading, time 


class SightwordsGame(Screen): 
    word = StringProperty('Screen1') 

    def SwitchToSettings(self): 
     self.manager.current = 'settings' 
     SettingsScreen = self.manager.get_screen('settings') 

class SettingsScreen(Screen): 

    def poptest(self): 
     time.sleep(5) 

    def ClearDatabase(self): 
     self.popup = Popup(title='Test popup', content=Image(source='waiting.gif'), 
          auto_dismiss=False, size_hint=(None, None), size=(400, 400)) 
     self.popup.open() 
     mythread = threading.Thread(target=self.poptest()) 
     mythread.start() 
     self.popup.dismiss() 

class SightwordsApp(App): 
    def build(self): 
     sm = ScreenManager() 
     screen1 = SightwordsGame(name='main') 
     screen2 = SettingsScreen(name='settings') 
     sm.add_widget(screen1) 
     sm.add_widget(screen2) 

     return sm 

if __name__ == '__main__': 
    SightwordsApp().run() 

sightwords.kv

#:kivy 1.9.1 

<[email protected]>: 
    font_size:40 
    color: 0,1,0,1 
    size_hint: 0.3, 0.2 

<SightwordsGame>: 
    AnchorLayout: 
     anchor_x: "center" 
     anchor_y: "center" 
     Label: 
      font_size: root.width/4 
      text: root.word 
    AnchorLayout: 
     anchor_x: "left" 
     anchor_y: "bottom" 
     Image: 
      source:'gear.png' 
      size_hint: 0.1,0.1 
      on_touch_down: if self.collide_point(*args[1].pos): root.SwitchToSettings() 

<SettingsScreen>: 
    GridLayout: 
     cols: 2 
     row_force_default: True 
     row_default_height: root.height/6 
     Label: 
      text: "Clear database:" 
      size_hint_x:None 
      width:root.width/3 
     Button: 
      text: "Press here to clear" 
      on_press: root.ClearDatabase() 
    AnchorLayout: 
     anchor_x: "left" 
     anchor_y: "bottom" 
     Image: 
      source: 'arrow.png' 
      on_touch_down: if self.collide_point(*args[1].pos): root.manager.current = 'main' 
      size_hint: 0.1,0.1 

回答

0

你開始線程,並立即關閉彈出窗口。當線程結束時應該關閉彈出窗口,因此您應該從線程關閉它:

class SettingsScreen(Screen): 

    def poptest(self): 
     time.sleep(5) 
     self.popup.dismiss() 

    def ClearDatabase(self): 
     self.popup = Popup(title='Test popup', content=Image(source='waiting.gif'), 
          auto_dismiss=False, size_hint=(None, None), size=(400, 400)) 
     self.popup.open() 
     mythread = threading.Thread(target=self.poptest) 
     mythread.start() 
+0

感謝您的迴應。你的回答與我原來的問題一致(我引用的帖子)(https://stackoverflow.com/questions/30595908/building-a-simple-progress-bar-or-loading-animation-in-kivy)。不幸的是,當我將self.popup.dismiss()移動到poptest()方法時,我得到了確切的結果 - 彈出窗口在5秒鐘之後閃爍。 – Zederick

+0

@Zederick這很奇怪......我已經在Python 2.7.13/Kivy 1.9.1和Python 3.6.1/Kivi 1.10中測試過代碼,我不能重現這個問題。按預期彈出窗口在屏幕上保持5秒鐘。 – FJSevilla

+0

@Zederick對不起,我忽略了一個錯誤:您必須將函數名稱傳遞給'target'參數(不應該使用圓括號)。我在答案中糾正了它。 – FJSevilla