我需要帮助在屏幕之间移动。我需要从MainWindow的5个屏幕(MainWindow,Button1,Button2,Button3,Button4)移动到Button屏幕,然后从按钮屏幕移回MainWindow。当我按下4个按钮之一时,我有问题控制台返回错误。
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.core.window import Window
Window.size = (400,650)
class MainWindow(FloatLayout):
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
# Main Label
label_position = AnchorLayout(anchor_x='center',
anchor_y='top')
label_settings = Label(text='> > > MyOrganiser MainWindow < < <',
size=(200, 50),
size_hint=(None, None))
label_position.add_widget(label_settings)
self.add_widget(label_position)
# Main Menu button 1
button_position = AnchorLayout(anchor_x='left',
anchor_y='center')
button_settings = Button(text='button1',
size=(200, 200),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=Button1)
# Main Menu button 2
button_position = AnchorLayout(anchor_x='right',
anchor_y='center')
button_settings = Button(text='button2',
size=(200, 200),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=Button2)
# Main Menu button 3
button_position = AnchorLayout(anchor_x='left',
anchor_y='bottom')
button_settings = Button(text='button3',
size=(200, 200),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=Button3)
# Main Menu button 4
button_position = AnchorLayout(anchor_x='right',
anchor_y='bottom')
button_settings = Button(text='button4',
size=(200, 200),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=Button4)
class Button1(FloatLayout):
def __init__(self, **kwargs):
super(Button1, self).__init__(**kwargs)
# Window Button1 Label
label_position = AnchorLayout(anchor_x='center',
anchor_y='top')
label_settings = Label(text='> > > MyOrganiser Button1 < < <',
size=(200, 50),
size_hint=(None, None))
label_position.add_widget(label_settings)
self.add_widget(label_position)
# back button for Button1 window
button_position = AnchorLayout(anchor_x='left',
anchor_y='bottom')
button_settings = Button(text='back',
size=(100, 50),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=MainWindow)
class Button2(FloatLayout):
def __init__(self, **kwargs):
super(Button2, self).__init__(**kwargs)
# Window Button2 Label
label_position = AnchorLayout(anchor_x='center',
anchor_y='top')
label_settings = Label(text='> > > MyOrganiser Button2 < < <',
size=(200, 50),
size_hint=(None, None))
label_position.add_widget(label_settings)
self.add_widget(label_position)
# back button for Button2 window
button_position = AnchorLayout(anchor_x='left',
anchor_y='bottom')
button_settings = Button(text='back',
size=(100, 50),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=MainWindow)
class Button3(FloatLayout):
def __init__(self, **kwargs):
super(Button3, self).__init__(**kwargs)
# Window Button3 Label
label_position = AnchorLayout(anchor_x='center',
anchor_y='top')
label_settings = Label(text='> > > MyOrganiser Button3 < < <',
size=(200, 50),
size_hint=(None, None))
label_position.add_widget(label_settings)
self.add_widget(label_position)
# back button for Button3 window
button_position = AnchorLayout(anchor_x='left',
anchor_y='bottom')
button_settings = Button(text='back',
size=(100, 50),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=MainWindow)
class Button4(FloatLayout):
def __init__(self, **kwargs):
super(Button4, self).__init__(**kwargs)
# Window Button4 Label
label_position = AnchorLayout(anchor_x='center',
anchor_y='top')
label_settings = Label(text='> > > MyOrganiser Button4 < < <',
size=(200, 50),
size_hint=(None, None))
label_position.add_widget(label_settings)
self.add_widget(label_position)
# back button for Button4 window
button_position = AnchorLayout(anchor_x='left',
anchor_y='bottom')
button_settings = Button(text='back',
size=(100, 50),
size_hint=(None, None))
button_position.add_widget(button_settings)
self.add_widget(button_position)
button_settings.bind(on_press=MainWindow)
class MyOrganiser(App):
def build(self):
return MainWindow()
if __name__ == '__main__':
MyOrganiser().run()
答案 0 :(得分:0)
您可以使用ScreenManager和Screen进行此操作 一个例子:
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
class MainWindow(Screen):
pass
class Button1(Screen):
pass
root = Builder.load_string('''
<MainWindow>:
name: "mainwindow"
FloatLayout:
Button:
text: "Goto Button1"
on_release: root.manager.current = "button1"
<Button1>:
name: "button1"
FloatLayout:
Button:
text: "Goto MainWindow"
on_release: root.manager.current = "mainwindow"
ScreenManager:
MainWindow:
Button1:
''')
class MyApp(App):
def build(self):
return root
MyApp().run()
在仅限Python的版本中,根据OP的要求:
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class MainWindow(Screen):
def __init__(self,**kwargs):
super(MainWindow,self).__init__(**kwargs)
self.name = "mainwindow"
self.fl = FloatLayout()
self.button = Button(text="Goto Button1")
self.button.bind(on_release=self.goto)
self.fl.add_widget(self.button)
self.add_widget(self.fl)
def goto(self,*args):
self.manager.current = "button1"
class Button1(Screen):
def __init__(self,**kwargs):
super(Button1,self).__init__(**kwargs)
self.name = "button1"
self.fl = FloatLayout()
self.button = Button(text="Goto MainWindow")
self.button.bind(on_release=self.goto)
self.fl.add_widget(self.button)
self.add_widget(self.fl)
def goto(self,*args):
self.manager.current = "mainwindow"
class MyApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(MainWindow())
sm.add_widget(Button1())
return sm
MyApp().run()