<tfoot id='pueHL'></tfoot>

    1. <i id='pueHL'><tr id='pueHL'><dt id='pueHL'><q id='pueHL'><span id='pueHL'><b id='pueHL'><form id='pueHL'><ins id='pueHL'></ins><ul id='pueHL'></ul><sub id='pueHL'></sub></form><legend id='pueHL'></legend><bdo id='pueHL'><pre id='pueHL'><center id='pueHL'></center></pre></bdo></b><th id='pueHL'></th></span></q></dt></tr></i><div id='pueHL'><tfoot id='pueHL'></tfoot><dl id='pueHL'><fieldset id='pueHL'></fieldset></dl></div>
          <bdo id='pueHL'></bdo><ul id='pueHL'></ul>
        <legend id='pueHL'><style id='pueHL'><dir id='pueHL'><q id='pueHL'></q></dir></style></legend>
      1. <small id='pueHL'></small><noframes id='pueHL'>

      2. Kivy:从python代码改变屏幕

        时间:2023-10-10
      3. <i id='RvoqV'><tr id='RvoqV'><dt id='RvoqV'><q id='RvoqV'><span id='RvoqV'><b id='RvoqV'><form id='RvoqV'><ins id='RvoqV'></ins><ul id='RvoqV'></ul><sub id='RvoqV'></sub></form><legend id='RvoqV'></legend><bdo id='RvoqV'><pre id='RvoqV'><center id='RvoqV'></center></pre></bdo></b><th id='RvoqV'></th></span></q></dt></tr></i><div id='RvoqV'><tfoot id='RvoqV'></tfoot><dl id='RvoqV'><fieldset id='RvoqV'></fieldset></dl></div>
        <tfoot id='RvoqV'></tfoot>
            <bdo id='RvoqV'></bdo><ul id='RvoqV'></ul>
          • <small id='RvoqV'></small><noframes id='RvoqV'>

              <legend id='RvoqV'><style id='RvoqV'><dir id='RvoqV'><q id='RvoqV'></q></dir></style></legend>
                    <tbody id='RvoqV'></tbody>
                  本文介绍了Kivy:从python代码改变屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在组装一个 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.pyScreen2 切换回 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模板网!

                  上一篇:在 Kivy 中使用和移动小部件/按钮 下一篇:如何在 Kivy 的画布中引用孩子的 id?

                  相关文章

                  最新文章

                      <legend id='hvbvW'><style id='hvbvW'><dir id='hvbvW'><q id='hvbvW'></q></dir></style></legend>
                      <i id='hvbvW'><tr id='hvbvW'><dt id='hvbvW'><q id='hvbvW'><span id='hvbvW'><b id='hvbvW'><form id='hvbvW'><ins id='hvbvW'></ins><ul id='hvbvW'></ul><sub id='hvbvW'></sub></form><legend id='hvbvW'></legend><bdo id='hvbvW'><pre id='hvbvW'><center id='hvbvW'></center></pre></bdo></b><th id='hvbvW'></th></span></q></dt></tr></i><div id='hvbvW'><tfoot id='hvbvW'></tfoot><dl id='hvbvW'><fieldset id='hvbvW'></fieldset></dl></div>
                      <tfoot id='hvbvW'></tfoot>
                    1. <small id='hvbvW'></small><noframes id='hvbvW'>

                        <bdo id='hvbvW'></bdo><ul id='hvbvW'></ul>