自动切换到下一个屏幕,然后回到kivy和python

时间:2018-02-18 18:32:50

标签: python kivy

我有点困惑为什么这不起作用。我所拥有的简单版本如下。我需要的是屏幕管理器启动主屏幕,在加载时它将启动线程并做更新主屏幕标签的事情......但它也应该启动时钟。让我们说10秒后切换到第二个屏幕..直到这里它工作...但同样需要在第二个屏幕上发生,开始时钟和10秒后回到主屏幕,这将发生在后台,但在视觉上的屏幕不会改变。不知道发生了什么:(

osd.py:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock, mainthread
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
import MySQLdb
import threading

Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')


class PauseScreen(Screen):

    def on_enter(self):
        print('enter')
        Clock.schedule_once(self.switch_back, 5)

    def switch_back(self, *args):
        print('back')
        self.clear_widgets()
        self.parent.current = "OSD_screen"


class MainScreen(Screen):

    def on_enter(self):
        print('start threading')
        #Clock.schedule_once(self.update_thread, 0.1)
        Clock.schedule_once(self.switch, 5)

    # def __init__(self, **kwargs):
    #    super(MainScreen, self).__init__(**kwargs)
        #Clock.schedule_interval(self.update_thread, 20)
     #   Clock.schedule_once(self.update_thread, 0.1)
     #   Clock.schedule_once(self.switch, 5)

    def switch(self, *args):
        self.parent.current = "pause"

class ScreenManagement(ScreenManager):
    stop = threading.Event()
    pass

presentation = Builder.load_file("osd.kv")

class osdApp(App):
    def on_stop(self):
        self.root.stop.set()

    def build(self):
        return presentation


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

osd.kv

ScreenManagement:
    id: screen_manager
    MainScreen:
        id: main_screen
        name: 'OSD_screen'
        manager: 'screen_manager'
    PauseScreen:
        id: pause_screen
        name: 'pause'
        manager: 'screen_manager'

<MainScreen>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text_size: self.size
            text: 'MainScreen'
            font_size: 50
            bold: True
            halign: 'center'
            valign: 'top'
            size_hint: 1, .3

<PauseScreen>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Pause Screen'
            font_size: 25

1 个答案:

答案 0 :(得分:1)

文件osd.kv被加载了多倍,您可能会有不需要的行为(例如,您将拥有多个具有相同名称的Screen个实例)。您可以在kv文件中声明ScreenManagement作为规则,而不是使用Builder.load_file方法:

<强> osd.py

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.clock import Clock
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen
import threading

Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')


class PauseScreen(Screen):

    def on_enter(self):
        print('enter')
        Clock.schedule_once(self.switch_back, 5)

    def switch_back(self, *args):
        print('back')
        self.manager.current = "osd_screen"


class MainScreen(Screen):

    def on_enter(self):
        print('start threading')
        Clock.schedule_once(self.switch, 5)

    def switch(self, *args):
        self.manager.current = "pause"


class ScreenManagement(ScreenManager):
    stop = threading.Event()


class OsdApp(App):
    def on_stop(self):
        self.root.stop.set()

    def build(self):
        return ScreenManagement()


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

<强> osd.kv

<ScreenManagement>:
    id: screen_manager
    MainScreen:
        id: main_screen
        name: 'osd_screen'
    PauseScreen:
        id: pause_screen
        name: 'pause'

<MainScreen>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text_size: self.size
            text: 'MainScreen'
            font_size: 50
            bold: True
            halign: 'center'
            valign: 'top'
            size_hint: 1, .3

<PauseScreen>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Pause Screen'
            font_size: 25