問題描述
我正在組裝一個 Kivy 應用程序,但在解決如何在 python 代碼中任意選擇的點更改屏幕時遇到了一些問題.
I'm putting together a Kivy app and am having some problems working out how to change screens at an arbitrarily chosen point within the python code.
在下面的例子中,我想知道如何通過執行一個函數 my main.py
從 Screen2
切換回 Screen1
文件.
In the following example, I would like to know how to switch from Screen2
back to Screen1
by executing a function my main.py
file.
這是我的 main.py
:
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty
from functools import partial
import random
import time
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
def __init__(self,**kwargs):
super(ScreenTwo, self).__init__(**kwargs)
self.displayScreenThenLeave()
def displayScreenThenLeave(self):
# 'time.sleep(3)' is a stand-in for "execute some code here"
time.sleep(3)
self.changeScreen()
# I want this function to send the user back to ScreenOne.
def changeScreen(self):
pass
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
這是我的 screens.kv
:
#:kivy 1.8.0
<ScreenOne>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
Label:
text: "Main Menu"
Button:
text: "Button 1"
on_release: root.manager.current = "screen2"
<ScreenTwo>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
Label:
id: label
text: "Welcome to Screen 2, please wait three seconds"
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen2"
manager: screen_manager
應該很明顯,我是 Kivy 的初學者,所以如果你能告訴我我需要更改的確切內容,包括應該使用的特定語法,我將不勝感激.
As should be pretty evident, I'm a total beginner at Kivy, so I'd really appreciate it if you could show me exactly what I need to change, including the specific syntax that should be used.
提前感謝您的時間和智慧.
Thanks in advance for your time and wisdom.
推薦答案
這是一個基于您的附加信息的簡單游戲"示例.當玩家進入游戲畫面時,他們的生命值正在流血.當池達到零時,它們被送回菜單屏幕.
Here is a simple 'game' example based on your additional info. When a player enters the game screen, their health points are bleeding. When the pool reaches zero, they are sent back to menu screen.
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty
import time
import threading
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
health_points = NumericProperty(100)
def __init__(self, **kwargs):
super(ScreenTwo, self).__init__(**kwargs)
def on_health_points(self, instance, value):
if value < 1:
self.changeScreen()
def on_enter(self, *args):
thread = threading.Thread(target=self.bleed)
thread.daemon = True
thread.start()
def bleed(self, *args):
while self.health_points > 0:
self.health_points -= 5
time.sleep(0.1)
def displayScreenThenLeave(self):
self.changeScreen()
def changeScreen(self):
if self.manager.current == 'screen1':
self.manager.current = 'screen2'
else:
self.manager.current = 'screen1'
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
screens.kv:
screens.kv:
#:kivy 1.8.0
<ScreenOne>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
Label:
text: "Main Menu"
Button:
text: "Button 1"
on_release:
root.manager.current = "screen2"
# reset health_points
root.manager.ids.screen_two.health_points = 100
<ScreenTwo>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
Label:
id: label
text: "health points: " + str(root.health_points)
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen2"
manager: screen_manager
這篇關于Kivy:從python代碼改變屏幕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!