我正在尝试编写一个允许用户清除数据库的kivy应用程序,实际上是将应用程序重置为默认值。我遇到的问题是,当用户点击按钮清除并重置数据库时("清除数据库"设置屏幕上的按钮),这是一个漫长的过程,我想要一个弹出框来显示一个旋转的gif,直到过程结束。
问题是弹出窗口和cleardatabase进程似乎同时发生,因此弹出窗口只会闪烁,然后在清除数据库进程结束时消失。
我已经探索了不同的帖子,并在其中尝试了解决方案,但似乎没有任何效果。
下面我包含了从我的实际代码派生的精简代码,该代码复制了问题,cleardatabase方法调用sleep(5)来模仿长进程。在最近的这次尝试中,我在this post中的解决方案之后对代码进行了建模。也许问题是我使用ScreenManager?
顺便说一下,这一切都在python2.7中,而我认为kivy是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
<MenuButton@Button>:
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 :(得分:0)
启动线程并立即关闭弹出窗口。你应该在线程结束时关闭弹出窗口,所以你应该从线程中关闭它:
records.sort(key=lambda k: k.name)
for item in records:
print item
# filerecord(date='17-01-01', name='a.ma', path='c:/test')
# filerecord(date='16-01-01', name='b.ma', path='c/test')
# filerecord(date='17-02-01', name='c.ma', path='c:/other')
# filerecord(date='17-06-30', name='d.ma', path='d:/extra')