我如何在kivy中切换屏幕?
How do I switch screens in 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'
我尝试了多种解决方案都没有进展。我的猜测是,因为我试图从我屏幕内的一个小部件切换屏幕 class 我得到了前面讨论的错误。我最初决定在每个屏幕 class 中放置一个小部件来组织元素,因为当我试图在 .kv
文件中为屏幕 class 独立地组织元素时(PulseWelcome 等)我有图形用户界面问题。
我也曾尝试在屏幕 class 中定义一个可以通过包含的小部件激活的功能。尝试之后,我得到了这个错误:
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
不确定是否有人知道可能导致问题的原因以及解决方法,但如果有人对前面提到的任何问题有任何意见,我们将不胜感激。
你还没有初始化你返回屏幕的屏幕管理器而不是屏幕管理器
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
class PulseWelcome(Screen):
pass
class PulseWelcomeCanvas(Widget):
pass
class PulseLogin(Screen):
class PulseLoginCanvas(Widget):
pass
class WindowManager(ScreenManager):
pass
class PulseApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(PulseWelcome(name='welcome'))
sm.add_widget(PulseLogin(name='login'))
return sm
if __name__ == '__main__':
PulseApp().run()
像这样定义构建函数应该可以解决问题。
.kv:
<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
我还从 kv 文件中删除了 screenmanager,因为它在创建 screenmanger 时不起作用,就像我在这里所做的那样。否则你将不得不像这样创建构建函数
构建:
def build:
returm WindowManager()
我 运行 在执行这样的构建功能时遇到了一些麻烦,所以现在我会按照我第一次向您展示的方式来做
希望对您有所帮助
我目前在我的代码中遇到无法切换屏幕的问题。在我解释具体问题之前,我将在下面包含我的代码片段。
来自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'
我尝试了多种解决方案都没有进展。我的猜测是,因为我试图从我屏幕内的一个小部件切换屏幕 class 我得到了前面讨论的错误。我最初决定在每个屏幕 class 中放置一个小部件来组织元素,因为当我试图在 .kv
文件中为屏幕 class 独立地组织元素时(PulseWelcome 等)我有图形用户界面问题。
我也曾尝试在屏幕 class 中定义一个可以通过包含的小部件激活的功能。尝试之后,我得到了这个错误:
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
不确定是否有人知道可能导致问题的原因以及解决方法,但如果有人对前面提到的任何问题有任何意见,我们将不胜感激。
你还没有初始化你返回屏幕的屏幕管理器而不是屏幕管理器
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
class PulseWelcome(Screen):
pass
class PulseWelcomeCanvas(Widget):
pass
class PulseLogin(Screen):
class PulseLoginCanvas(Widget):
pass
class WindowManager(ScreenManager):
pass
class PulseApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(PulseWelcome(name='welcome'))
sm.add_widget(PulseLogin(name='login'))
return sm
if __name__ == '__main__':
PulseApp().run()
像这样定义构建函数应该可以解决问题。
.kv:
<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
我还从 kv 文件中删除了 screenmanager,因为它在创建 screenmanger 时不起作用,就像我在这里所做的那样。否则你将不得不像这样创建构建函数
构建:
def build:
returm WindowManager()
我 运行 在执行这样的构建功能时遇到了一些麻烦,所以现在我会按照我第一次向您展示的方式来做
希望对您有所帮助