使用微调器和按钮一起更改屏幕

时间:2018-04-03 19:42:54

标签: python button kivy spinner screen

我找到了this代码,向我展示了如何使用微调器来更改屏幕。它在允许我切换屏幕方面效果很好,但我发现我还需要使用单个屏幕上的按钮来切换屏幕。

这是我的意思的一个例子:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner

KV = """
<MainScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Edit'
            on_press: root.goEdit()
        Label:
            text: 'Main Screen'

<HelpScreen>:
    Label:
        text: 'Help Screen'

<SettingsScreen>:
    Label:
        text: 'Settings Screen'

<EditScreen>:
    name: 'edit_Screen'
    text: 'Edit Screen'

<ScreenMenu>:
    text: 'main'
    values: ('main', 'help', 'settings')
    size_hint: None, None
    size: 200, 44
"""


class MainScreen(FloatLayout):

    def goEdit(self):
        MyApp.build.screen_layout.remove_widget(MyApp.screen)
        screen = EditScreen()
        MyApp.screen = screen
        MyApp.screen_layout.add_widget(MyApp.screen)


class HelpScreen(FloatLayout):
    pass

class SettingsScreen(FloatLayout):
    pass

class EditScreen(FloatLayout):
    pass

class ScreenMenu(Spinner):
    pass


class MyApp(App):

    def build(self):
        Builder.load_string(KV)
        self.screen = None
        self.root = FloatLayout()
        self.screen_layout = FloatLayout()
        self.menu = ScreenMenu()
        self.root.add_widget(self.screen_layout)
        self.root.add_widget(self.menu)

        self.menu.bind(text=self.select_screen)
        self.show('main')
        return self.root

    def select_screen(self, *args):
        self.show(self.menu.text)

    def show(self, name='main'):
        if self.screen is not None:
            self.screen_layout.remove_widget(self.screen)
            self.screen = None
        if name == 'main':
            screen = MainScreen()
        elif name == 'help':
            screen = HelpScreen()
        elif name == 'settings':
            screen = SettingsScreen()
        else:
            raise Exception('Invalid screen name')
        self.screen = screen
        self.screen_layout.add_widget(screen)


if __name__ == "__main__":
    MyApp().run()

正如您所看到的,这几乎是上面链接中的确切代码,唯一的区别是,在这里,我创建了一个“编辑”按钮,单击该按钮时,指的是“MainScreen”的goEdit()方法“上课。

on_press: root.goEdit()

我的问题是我不知道如何创建goEdit()方法,以便在调用时,它进入“EditScreen”同时还有微调器工作(它转到“MainScreen”,“HelpScreen”和“SettingScreen”屏幕)。我在goEdit()中尝试的代码显然不起作用。

我也尝试将继承的类从FloatLayout更改为Screen:

class MainScreen(Screen):   
    def goEdit(self):
        self.parent.current = 'edit_Screen'         
class HelpScreen(Screen):
    pass
class SettingsScreen(Screen):
    pass
class EditScreen(Screen):
    pass
class ScreenMenu(Spinner):
    pass

在这里,我尝试使用以下代码切换屏幕:

self.parent.current = 'edit_Screen'

但是当点击“编辑”按钮时,没有任何反应,我甚至都没有收到错误消息。

基本上我想要的是微调器像示例链接一样工作,但我还需要“编辑”按钮来更改屏幕。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

以下是使用ScreenManager的一个非常简单的示例:

<强>的Python

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

class Screen1(Screen):

    pass

class Screen2(Screen):

    pass

class Screen3(Screen):

    pass

class MyScreenManager(ScreenManager):

    def changescreen(self, value):

        try:
            if value!='Choose a Value...':
                self.current = value
        except:
            print('No screen named ' + value)

#Main application
class TestApp(App):

    def build(self):
        self.sm = MyScreenManager()
        return self.sm

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

<强> KV

<MyScreenManager>:

    Screen1:
        name: 'screen1'
    Screen2:
        name: 'screen2'
    Screen3:
        name: 'screen3'

<Screen1>:

    GridLayout:

        rows: 2
        padding: 20
        spacing: 20

        Button:
            text: 'Go to Screen 2'
            on_press: root.manager.current = 'screen2'

        Button:
            text: 'Go to Screen 3'
            on_press: root.manager.current = 'screen3'

        Spinner:
            text: 'Choose a Value...'
            values: ['screen2', 'screen3']
            on_text:
                root.manager.changescreen(self.text)
                self.text = 'Choose a Value...'

        Label:
            text: 'You are on ' + root.name

<Screen2>:

    GridLayout:

        rows: 2
        padding: 20
        spacing: 20

        Button:
            text: 'Go to Screen 1'
            on_press: root.manager.current = 'screen1'

        Button:
            text: 'Go to Screen 3'
            on_press: root.manager.current = 'screen3'

        Spinner:
            text: 'Choose a Value...'
            values: ['screen1', 'screen3']
            on_text:
                root.manager.changescreen(self.text)
                self.text = 'Choose a Value...'

        Label:
            text: 'You are on ' + root.name

<Screen3>:

    GridLayout:

        rows: 2
        padding: 20
        spacing: 20

        Button:
            text: 'Go to Screen 1'
            on_press: root.manager.current = 'screen1'

        Button:
            text: 'Go to Screen 2'
            on_press: root.manager.current = 'screen2'

        Spinner:
            text: 'Choose a Value...'
            values: ['screen1', 'screen2']
            on_text:
                root.manager.changescreen(self.text)
                self.text = 'Choose a Value...'

        Label:
            text: 'You are on ' + root.name

答案 1 :(得分:0)

解决方案

有关详细信息,请参阅说明,示例和输出。

kv String

  1. root.edit()替换为 app.show(name ='edit')
  2. 使用标签替换名称:'edit_screen'
  3. 缩进文字:'编辑屏幕'
  4. Python脚本

    1. 传递
    2. 替换 MainScreen 中的所有编码
    3. 在方法 show
    4. 中添加 elif name =='edit':screen = EditScreen()语句

      实施例

      main.py

      from kivy.app import App
      from kivy.lang import Builder
      from kivy.uix.floatlayout import FloatLayout
      from kivy.uix.spinner import Spinner
      
      KV = """
      <MainScreen>:
          BoxLayout:
              orientation: 'vertical'
              Button:
                  text: 'Edit'
                  on_press: app.show(name='edit')
              Label:
                  text: 'Main Screen'
      
      <HelpScreen>:
          Label:
              text: 'Help Screen'
      
      <SettingsScreen>:
          Label:
              text: 'Settings Screen'
      
      <EditScreen>:
          Label:
              text: 'Edit Screen'
      
      <ScreenMenu>:
          text: 'main'
          values: ('main', 'help', 'settings')
          size_hint: None, None
          size: 200, 44
      """
      
      
      class MainScreen(FloatLayout):
          pass
      
      
      class HelpScreen(FloatLayout):
          pass
      
      
      class SettingsScreen(FloatLayout):
          pass
      
      
      class EditScreen(FloatLayout):
          pass
      
      
      class ScreenMenu(Spinner):
          pass
      
      
      class MyApp(App):
      
          def build(self):
              Builder.load_string(KV)
              self.screen = None
              self.root = FloatLayout()
              self.screen_layout = FloatLayout()
              self.menu = ScreenMenu()
              self.root.add_widget(self.screen_layout)
              self.root.add_widget(self.menu)
      
              self.menu.bind(text=self.select_screen)
              self.show('main')
              return self.root
      
          def select_screen(self, *args):
              self.show(self.menu.text)
      
          def show(self, name='main'):
              if self.screen is not None:
                  self.screen_layout.remove_widget(self.screen)
                  self.screen = None
              if name == 'main':
                  screen = MainScreen()
              elif name == 'help':
                  screen = HelpScreen()
              elif name == 'settings':
                  screen = SettingsScreen()
              elif name == 'edit':
                  screen = EditScreen()
              else:
                  raise Exception('Invalid screen name')
              self.screen = screen
              self.screen_layout.add_widget(screen)
      
      
      if __name__ == "__main__":
          MyApp().run()
      

      输出

      Img01 - MainScreen @ AppStartup Img02 - Spinner @ MainScreen Img03 - EditScreen