Kivy 在按下一个按钮时,会创建一个新按钮,该按钮将转到第二个屏幕
Kivy On pressing a Button, a New Button is created which takes to Second Screen
我有一个代码,其中有两个屏幕:“主”和“第二”,当您在主屏幕中按下一个按钮时,会在同一屏幕中创建一个新按钮。我需要创建新按钮以将我带到第二个屏幕。请帮忙,因为我需要它来完成一个更大的项目。
普通代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen,ScreenManager
class Main(Screen):
pass
class Second(Screen):
pass
class Manager(ScreenManager):
pass
kv=Builder.load_file("test1.kv")
movie=Manager()
movie.add_widget(Main(name="main"))
movie.add_widget(Second(name="second"))
class Movie(App):
def build(self):
return movie
Movie().run()
Kv代码:
#: import Button kivy.uix.button.Button
<Main>:
name: "main"
GridLayout:
id: GL
cols: 1
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5})
GL.add_widget(Btn)
#Btn.bind(on_press= ????)
<Second>:
name: "second"
Button:
text: "Go Back!"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
on_press:
app.root.current="main"
最简单的方法是在 Main
class 中定义一个方法来执行 Screen
更改:
class Main(Screen):
def switch(self, button_instance):
self.manager.current = 'second'
然后在 kv
中引用该方法:
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5}, on_press=root.switch)
GL.add_widget(Btn)
一种不需要任何额外方法的更复杂的方法通过使用 python setattr()
方法来设置 [=20] 的 current
Screen
=]:
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5})
Btn.bind(on_press=lambda ins: partial(setattr, root.manager, 'current', 'second')())
GL.add_widget(Btn)
需要 lambda
以避免将 Button
实例传递给 setattr()
方法。此外,kv
中的 partial
需要导入:
#: import partial functools.partial
我有一个代码,其中有两个屏幕:“主”和“第二”,当您在主屏幕中按下一个按钮时,会在同一屏幕中创建一个新按钮。我需要创建新按钮以将我带到第二个屏幕。请帮忙,因为我需要它来完成一个更大的项目。
普通代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen,ScreenManager
class Main(Screen):
pass
class Second(Screen):
pass
class Manager(ScreenManager):
pass
kv=Builder.load_file("test1.kv")
movie=Manager()
movie.add_widget(Main(name="main"))
movie.add_widget(Second(name="second"))
class Movie(App):
def build(self):
return movie
Movie().run()
Kv代码:
#: import Button kivy.uix.button.Button
<Main>:
name: "main"
GridLayout:
id: GL
cols: 1
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5})
GL.add_widget(Btn)
#Btn.bind(on_press= ????)
<Second>:
name: "second"
Button:
text: "Go Back!"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
on_press:
app.root.current="main"
最简单的方法是在 Main
class 中定义一个方法来执行 Screen
更改:
class Main(Screen):
def switch(self, button_instance):
self.manager.current = 'second'
然后在 kv
中引用该方法:
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5}, on_press=root.switch)
GL.add_widget(Btn)
一种不需要任何额外方法的更复杂的方法通过使用 python setattr()
方法来设置 [=20] 的 current
Screen
=]:
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5})
Btn.bind(on_press=lambda ins: partial(setattr, root.manager, 'current', 'second')())
GL.add_widget(Btn)
需要 lambda
以避免将 Button
实例传递给 setattr()
方法。此外,kv
中的 partial
需要导入:
#: import partial functools.partial