1

I'm bit confused why this is not working. Simple version of what I have is bellow. What I need is to screen manager start the main screen, on load it will start thread and do it thing updating the main screen labels... but also it should start clock. Lets say after 10 sec switch to second screen.. Until here it works... but the same needs to happen on the second screen, start clock and after 10 sec go back to main screen, this will happen in background but visually the screen will not change. Not sure what is going on :(

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 Answer 1

1

The file osd.kv is loaded multiples times, you might have unwanted behaviors (For example, you will have several Screen instances with the same name). You can declare ScreenManagement as a rule in your kv file and not use Builder.load_file method:

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
2
  • Thanks man, I was seeing the message that is loaded multiple times but did not understand why that is. Also renaming the kv file and loading it under different name as ...App worked fine. Many thanks ! Feb 19, 2018 at 7:13
  • 1
    kv file is loaded twice because an explicit call is made to Builder.load_file but, in addition, it is automatically loaded when instantiating osdApp (Kivy looks for a Kv file with the same name as App class in lowercase), check How to load kv?.
    – FJSevilla
    Feb 19, 2018 at 7:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.