如何在Kivy切换屏幕?
我目前在代码中遇到问题,无法切换屏幕。在我说明问题是什么之前,我将在下面包括我的代码段。
来自main.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
Window.clearcolor = 255 / 255, 255 / 255, 255 / 255, 1
Window.size = 414, 736
sm = ScreenManager()
class PulseWelcome(Screen):
pass
class PulseWelcomeCanvas(Widget):
pass
class PulseLogin(Screen):
class PulseLoginCanvas(Widget):
pass
class WindowManager(ScreenManager):
pass
sm.add_widget(PulseWelcome(name = 'welcome'))
sm.add_widget(PulseLogin(name = 'login'))
class PulseApp(App):
def build(self):
return PulseWelcome()
if __name__ == '__main__':
PulseApp().run()
来自pulse.kv
#:kivy 2.1.0
WindowManager:
PulseWelcome:
PulseLogin:
<PulseWelcome>:
id: welcome
PulseWelcomeCanvas:
Label:
font_size: 30
font_name: 'assets/fonts/tommy.ttf'
center_x: root.width / 2
center_y: root.height / 2
text: "Welcome to Pulse!"
color: 0, 0, 0, 1
Button:
background_normal: 'assets/images/next_purple_normal.png'
background_down: 'assets/images/next_purple_down.png'
border: 0, 0, 0, 0
center_x: root.width / 2
center_y: 90
height: 40
width: 100
on_press:
root.manager.transition.direction = 'left'
root.manager.current = 'login'
<PulseLogin>:
id: login
PulseLoginCanvas:
Label:
font_size: 30
font_name: 'assets/fonts/tommy.ttf'
text: 'Sign In or Sign Up'
color: 0, 0, 0, 1
center_x: root.width / 2
center_y: root.height / 2
我的应用程序在程序启动时应运行,但是当我单击按钮以更改屏幕时,我被介绍了有了这个错误:
File "C:\Users\Nitro\Documents\Pulse\pulse.kv", line 30, in <module>
root.manager.transition.direction = 'left'
AttributeError: 'NoneType' object has no attribute 'transition'
我尝试了各种解决方案而没有进步。我的猜测是,因为我试图将屏幕从屏幕类内的小部件切换,所以我给出了较早的讨论错误。我最初决定在每个屏幕类中放一个小部件以整理元素,因为当我试图在.kv
文件中组织屏幕类的元素时(pulsewelcome等),我遇到了GUI问题。
我还尝试在屏幕类中定义一个可以通过包含的小部件激活的函数。经过尝试后,我得到了这个错误:
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
不确定是否有人知道可能导致问题以及如何解决问题,但是如果有人对之前提到的任何问题都有任何类型的输入,那么所有帮助都将非常感谢。
I am currently having an issue in my code where I am unable to switch screens. Before I explain what the issue is specifically, I will include a snippet of my code below.
From main.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
Window.clearcolor = 255 / 255, 255 / 255, 255 / 255, 1
Window.size = 414, 736
sm = ScreenManager()
class PulseWelcome(Screen):
pass
class PulseWelcomeCanvas(Widget):
pass
class PulseLogin(Screen):
class PulseLoginCanvas(Widget):
pass
class WindowManager(ScreenManager):
pass
sm.add_widget(PulseWelcome(name = 'welcome'))
sm.add_widget(PulseLogin(name = 'login'))
class PulseApp(App):
def build(self):
return PulseWelcome()
if __name__ == '__main__':
PulseApp().run()
From Pulse.kv
#:kivy 2.1.0
WindowManager:
PulseWelcome:
PulseLogin:
<PulseWelcome>:
id: welcome
PulseWelcomeCanvas:
Label:
font_size: 30
font_name: 'assets/fonts/tommy.ttf'
center_x: root.width / 2
center_y: root.height / 2
text: "Welcome to Pulse!"
color: 0, 0, 0, 1
Button:
background_normal: 'assets/images/next_purple_normal.png'
background_down: 'assets/images/next_purple_down.png'
border: 0, 0, 0, 0
center_x: root.width / 2
center_y: 90
height: 40
width: 100
on_press:
root.manager.transition.direction = 'left'
root.manager.current = 'login'
<PulseLogin>:
id: login
PulseLoginCanvas:
Label:
font_size: 30
font_name: 'assets/fonts/tommy.ttf'
text: 'Sign In or Sign Up'
color: 0, 0, 0, 1
center_x: root.width / 2
center_y: root.height / 2
My app runs as it should when the program is started, however, when I click on my button in order to change screens I am presented with this error:
File "C:\Users\Nitro\Documents\Pulse\pulse.kv", line 30, in <module>
root.manager.transition.direction = 'left'
AttributeError: 'NoneType' object has no attribute 'transition'
I have tried a variety of solutions with no progress. My guess is that because I am trying to switch screens from a widget inside my screen class I am given the earlier discussed error. I originally decided to put a widget inside each screen class to organize elements because when I had tried to organize elements in the .kv
file for the screen class independently (PulseWelcome, etc.) I had gui issues.
I also had tried defining a function in the screen class that could be activated through the contained widget. After trying that I was given this error:
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
Not sure if anyone out there knows what may be causing the issue and how to fix it but if anyone has any sort of input for any of the issues previously mentioned all help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未初始化ScreenManager返回屏幕,而不是ScreenManager
main.py:
定义这样的构建功能应该可以解决问题。
.KV:
我还从KV文件中删除了ScreenManager,因为在创建ScreenManger时,就像我在这里所做的那样。否则,您将不得不创建像这样的构建函数
:
我在这样做的构建功能上遇到了一些麻烦,所以现在我会以我第一次向您展示的方式做到这一点,
希望这会有所帮助
You have not initialized the screenmanager you returning the screen and not the screenmanager
main.py:
defining the build function like this should do the trick.
.kv:
I have also removed the screenmanager from the kv file as that wouldn't function when creating the screenmanger like I have done here. Otherwise you would have to create the build function like this
build:
I have run in to some trouble with doing the build function like that so for now I would do it the way I first showed you
Hope this helps