(Kivy Python)在 .py 文件中按下按钮时切换屏幕
Posted
技术标签:
【中文标题】(Kivy Python)在 .py 文件中按下按钮时切换屏幕【英文标题】:(Kivy Python) Switching Screens on Button Press Inside .py file 【发布时间】:2017-12-05 01:06:00 【问题描述】:我了解使用 on_release 在 .kv 文件中切换屏幕相对容易。但是,我想在 .py 文件中创建按钮,所以我不想使用这种方法。我已完成以下操作以添加按下第 14 个按钮时发生的功能。在程序中按下按钮时,没有任何反应。尝试将屏幕的其他名称设置为 sm.current 以引发错误:“kivy.uix.screenmanager.ScreenManagerException: No Screen with name "InputScreen" when the 14th button ispressed."
# Kivy Formatting
kv_text='''\
<MyScreenManager>:
LandingScreen:
InputScreen:
<InputScreen@Screen>:
name: 'input_sc'
AnchorLayout:
id: anchor_1
<LandingScreen@Screen>:
name: 'landing_sc'
GridLayout:
id: grid_1
cols: 5
height: 480
width: 800
spacing: 25, 20
padding: 25,25
'''
# Screen Manager
class MyScreenManager(ScreenManager):
pass
# Main screen with button layout
class LandingScreen(Screen):
def __init__(self, **kwargs):
super(LandingScreen, self).__init__(**kwargs)
self.buttons = [] # add references to all buttons here
Clock.schedule_once(self._finish_init)
# IDs have to be used here because they cannot be applied until widget initialized
def _finish_init(self, dt):
self.ids.grid_1.cols = 5
# Loop to make 15 different buttons on screen
for x in range(15):
self.buttons.append(Button(text='button '.format(x)))
self.ids.grid_1.add_widget(self.buttons[x])
self.buttons[x].background_normal = 'YOUTUBE.png'
def SwitchScreen(self,*args):
sm.current = 'input_sc'
sm = ScreenManager()
sm.add_widget(InputScreen(name='input_sc'))
sm.add_widget(LandingScreen(name='landing'))
self.buttons[14].bind(on_release=SwitchScreen)
# Input screen
class InputScreen(Screen):
pass
class MySubApp(App):
def build(self):
return MyScreenManager()
def main():
Builder.load_string(kv_text)
app = MySubApp()
app.run()
if __name__ == '__main__':
main()
如果有人可以帮助我理解当前逻辑中的漏洞,我将不胜感激。谢谢。
【问题讨论】:
【参考方案1】:每个屏幕都有一个manager
属性,它为您提供使用的ScreenManager
的实例。您只需要使用它来引用ScreemManager
实例并使用它的current
方法即可:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.clock import Clock
# Kivy Formatting
kv_text='''\
<MyScreenManager>:
LandingScreen:
InputScreen:
<InputScreen@Screen>:
name: 'input_sc'
AnchorLayout:
id: anchor_1
Button:
text: 'Hello'
<LandingScreen@Screen>:
name: 'landing_sc'
GridLayout:
id: grid_1
cols: 5
height: 480
width: 800
spacing: 25, 20
padding: 25,25
'''
# Screen Manager
class MyScreenManager(ScreenManager):
pass
# Main screen with button layout
class LandingScreen(Screen):
def __init__(self, **kwargs):
super(LandingScreen, self).__init__(**kwargs)
self.buttons = [] # add references to all buttons here
Clock.schedule_once(self._finish_init)
# IDs have to be used here because they cannot be applied until widget initialized
def _finish_init(self, dt):
self.ids.grid_1.cols = 5
# Loop to make 15 different buttons on screen
for x in range(15):
self.buttons.append(Button(text='button '.format(x)))
self.ids.grid_1.add_widget(self.buttons[x])
self.buttons[x].background_normal = 'YOUTUBE.png'
self.buttons[14].bind(on_release=self.switch_screen)
def switch_screen(self, *args):
self.manager.current = 'input_sc'
# Input screen
class InputScreen(Screen):
pass
class MySubApp(App):
def build(self):
return MyScreenManager()
def main():
Builder.load_string(kv_text)
app = MySubApp()
app.run()
if __name__ == '__main__':
main()
【讨论】:
感谢另一个很好的答案,我现在在文档中看到了。请原谅我,因为我是一般编程的新手,但是在像 Kivy 这样的系统中学习所有差异的最佳方法是什么?除了官方还有其他文件吗?或者你有没有纯粹通过经验学到你所知道的?谢谢 当我开始使用 Kivy 时,除了官方文档(这是一个相对较新的框架)之外,几乎没有什么信息。第一个来源始终是官方文档(Kivy Tutorials 非常适合入门),除此之外,您还可以在 YouTube、Kivy Google Groups 上找到教程、视频教程,当然还有Stack Overflow。别担心,学习编码的唯一真正方法是练习、犯错误并从中学习。顺便说一句,这是一个永无止境的学习过程...... :) @Austin以上是关于(Kivy Python)在 .py 文件中按下按钮时切换屏幕的主要内容,如果未能解决你的问题,请参考以下文章
在 Kivy for Python 中按下按钮时更新标签的文本