gpt4 book ai didi

python - 在 kivy 和 python 中自动切换到下一个屏幕并返回

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:25 24 4
gpt4 key购买 nike

我有点困惑为什么这不起作用。我所拥有的简单版本如下。我需要的是屏幕管理器启动主屏幕,加载时它将启动线程并更新主屏幕标签......但它也应该启动时钟。假设 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

最佳答案

文件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

关于python - 在 kivy 和 python 中自动切换到下一个屏幕并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com