python- .py 文件中的 Kivy 屏幕管理器
Posted
技术标签:
【中文标题】python- .py 文件中的 Kivy 屏幕管理器【英文标题】:python- Kivy Screen Manager within .py file 【发布时间】:2019-04-22 12:10:17 【问题描述】:我正在使用 Kivy 构建一个多屏幕应用程序,我想使用 ScreenManager 在多个屏幕之间导航。我已经看过有关如何在 .kv 文件中创建屏幕的示例和文档,但我想知道如何在 .py 文件中创建它们。
问题:当我创建如下所示的屏幕子类时,我的应用程序 窗口返回一个空白屏幕。 问题:正确的方法是什么 在 .py 文件中创建 Screen 子类?现在我定义了两个 Screen 子类:“welcomeScreen”和“functionScreen”。每个都包含一个带有一些小部件的布局。
kivy.require('1.9.1')
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
import kivy.uix.boxlayout
import kivy.uix.button
from kivy.uix.screenmanager import ScreenManager, Screen
class PanelBuilderApp(App): # display the welcome screen
def build(self):
# Create the screen manager and add widgets to the base sm widget
sm = kivy.uix.screenmanager.ScreenManager()
sm.add_widget(Screen(name='welcomeScreen'))
sm.add_widget(Screen(name='functionScreen'))
# sm.current= 'welcomeScreen'
return sm
class welcomeScreen(Screen): #welcomeScreen subclass
def __init__(self, **kwargs): #constructor method
super(welcomeScreen, self).__init__(**kwargs) #init parent
welcomePage = FloatLayout()
box = kivy.uix.boxlayout.BoxLayout(orientation='vertical', size_hint=(0.4, 0.3),
padding=8, pos_hint='top': 0.5, 'center_x': 0.5)
welcomeLabel = Label(text='Hello and welcome to the Panel Builder version 1.0.\nApp by John Vorsten\nClick below to continue',
halign= 'center', valign= 'center', size_hint= (0.4, 0.2), pos_hint= 'top': 1, 'center_x': 0.5)
welcomeBox = kivy.uix.button.Button(text= 'Click to continue')
welcomeBox.bind(on_press= self.callback)
welcomeBox2 = kivy.uix.button.Button(text='not used')
welcomePage.add_widget(welcomeLabel)
box.add_widget(welcomeBox)
box.add_widget(welcomeBox2)
welcomePage.add_widget(box)
self.add_widget(welcomePage)
def callback(instance):
print('The button has been pressed')
sm.switch_to(Screen(name= 'functionScreen'))
# sm.current = Screen(name= 'functionScreen')
class functionScreen(Screen): #For later function navigation
def __init__(self, **kwargs): #constructor method
super(functionScreen, self).__init__(**kwargs) #init parent
functionPage = kivy.uix.floatlayout.FloatLayout()
functionLabel = Label(text='Welcome to the function page. Here you will choose what functions to use',
halign='center', valign='center', size_hint=(0.4,0.2), pox_hint='top': 1, 'center_x': 0.5)
functionPage.add_widget(functionLabel)
self.add_widget(functionPage)
# sm.add_widget('Name') #Add more names later when you create more screens
# OR#
# for i in ScreenDirectory:
# sm.add_widget(ScreenDirectory[i])
PanelBuilderApp().run()
if __name__ == '__main__':
pass
我知道我可以将定义添加到 .kv 文件中,随着应用程序的增长,我可能会这样做。不过,在学习如何使用 kivy 时,我喜欢直言不讳。
【问题讨论】:
【参考方案1】:我认为你认为使用Screen(name='welcomeScreen')
是在使用welcomeScreen
,但事实并非如此,如果你想使用welcomeScreen
,你应该直接使用它。
另一方面,你有印刷错误,所以我已经更正了,我建议你遵循 kivy 教程,显然你必须有一个坚实的 OOP 基础(我认为你没有它,所以你的任务是加强)。
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
class PanelBuilderApp(App): # display the welcome screen
def build(self):
sm = ScreenManager()
sm.add_widget(WelcomeScreen(name='welcomeScreen'))
sm.add_widget(FunctionScreen(name='functionScreen'))
return sm
class WelcomeScreen(Screen): #welcomeScreen subclass
def __init__(self, **kwargs): #constructor method
super(WelcomeScreen, self).__init__(**kwargs) #init parent
welcomePage = FloatLayout()
box = BoxLayout(orientation='vertical', size_hint=(0.4, 0.3),
padding=8, pos_hint='top': 0.5, 'center_x': 0.5)
welcomeLabel = Label(text='Hello and welcome to the Panel Builder version 1.0.\nApp by John Vorsten\nClick below to continue',
halign= 'center', valign= 'center', size_hint= (0.4, 0.2), pos_hint= 'top': 1, 'center_x': 0.5)
welcomeBox = Button(text= 'Click to continue', on_press=self.callback)
welcomeBox2 = Button(text='not used')
welcomePage.add_widget(welcomeLabel)
box.add_widget(welcomeBox)
box.add_widget(welcomeBox2)
welcomePage.add_widget(box)
self.add_widget(welcomePage)
def callback(self, instance):
print('The button has been pressed')
self.manager.current = 'functionScreen'
class FunctionScreen(Screen): #For later function navigation
def __init__(self, **kwargs): #constructor method
super(FunctionScreen, self).__init__(**kwargs) #init parent
functionPage = FloatLayout()
functionLabel = Label(text='Welcome to the function page. Here you will choose what functions to use',
halign='center', valign='center', size_hint=(0.4,0.2), pos_hint='top': 1, 'center_x': 0.5)
functionPage.add_widget(functionLabel)
self.add_widget(functionPage)
if __name__ == '__main__':
PanelBuilderApp().run()
【讨论】:
以上是关于python- .py 文件中的 Kivy 屏幕管理器的主要内容,如果未能解决你的问题,请参考以下文章
如何在 python/kivy/pyjnius 检测 Android 中的屏幕分辨率?