基维切换按钮
Kivy Toggle Button
根据 Kivy docs。 "Toggle buttons can also be grouped to make radio buttons - only one button in a group can be in a ‘down’ state."
是否可以使用切换按钮让 1 个按钮使用屏幕管理器来回切换屏幕?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
root = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: (1, .1)
ToggleButton:
text: "Settings"
on_press: _screen_manager.current = 'settings'
BoxLayout:
orientation: 'vertical'
ScreenManager:
size_hint: 1, .8
id: _screen_manager
Screen:
name: 'game'
Screen:
name: 'settings'
BoxLayout:
orientation: 'vertical'
size_hint: (1, .1)
Button:
text: "Back"
on_press: _screen_manager.current = 'game'
BoxLayout:
orientation: 'vertical'
''')
class MyApp(App):
def build(self):
return root
MyApp().run()
您可以为此使用 on_state
方法。
试试这个:
from kivy.app import App
from kivy.lang import Builder
root = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
BoxLayout:
ToggleButton:
text: "Settings"
on_state: _screen_manager.current = 'settings' if self.state == 'down' else 'game'
BoxLayout:
orientation: 'vertical'
ScreenManager:
size_hint: 1, .8
id: _screen_manager
Screen:
name: 'game'
Label:
text: 'Game'
Screen:
name: 'settings'
Label:
text: 'Settings'
''')
class MyApp(App):
def build(self):
return root
MyApp().run()
根据 Kivy docs。 "Toggle buttons can also be grouped to make radio buttons - only one button in a group can be in a ‘down’ state."
是否可以使用切换按钮让 1 个按钮使用屏幕管理器来回切换屏幕?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
root = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: (1, .1)
ToggleButton:
text: "Settings"
on_press: _screen_manager.current = 'settings'
BoxLayout:
orientation: 'vertical'
ScreenManager:
size_hint: 1, .8
id: _screen_manager
Screen:
name: 'game'
Screen:
name: 'settings'
BoxLayout:
orientation: 'vertical'
size_hint: (1, .1)
Button:
text: "Back"
on_press: _screen_manager.current = 'game'
BoxLayout:
orientation: 'vertical'
''')
class MyApp(App):
def build(self):
return root
MyApp().run()
您可以为此使用 on_state
方法。
试试这个:
from kivy.app import App
from kivy.lang import Builder
root = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
BoxLayout:
ToggleButton:
text: "Settings"
on_state: _screen_manager.current = 'settings' if self.state == 'down' else 'game'
BoxLayout:
orientation: 'vertical'
ScreenManager:
size_hint: 1, .8
id: _screen_manager
Screen:
name: 'game'
Label:
text: 'Game'
Screen:
name: 'settings'
Label:
text: 'Settings'
''')
class MyApp(App):
def build(self):
return root
MyApp().run()