如何在kivy中从一个班级移动到另一个班级

时间:2015-02-24 06:18:43

标签: python-2.7 kivy

我正在尝试创建一个Android应用程序...我正在尝试根据用户输入从菜单切换到服务器或客户端...但我无法从一个类切换到另一个...主要的事情是生气的是,ServerScreen内部的print语句正在工作,但其他部分无效......不知道为什么

代码:---

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen



class MenuScreen(Screen):


    def __init__(self, **kwargs):

        super(MenuScreen, self).__init__(**kwargs)


        def Server(instance):
            self.clear_widgets()
            self.add_widget(Label ( text = 'Inside server function'))

            server = ServerScreen()
            #return server
            #server.function()

        self.add_widget(Label ( text = 'What Type Of Service You Want...???'))

        button1 = Button(text = 'Server',size_hint = (None,None),pos = (0,0))
        self.add_widget(button1)
        button1.bind(on_press = Server)

        button2 = Button(text = 'Client',size_hint = (None,None),pos = (100,0))
        self.add_widget(button2)
        #button2.bind(on_press = Client)




class ServerScreen(Screen):

    def __init__(self, **kwargs):
        super(ServerScreen, self).__init__(**kwargs)
        print('Inside server screen')

        self.clear_widgets()
        self.add_widget(Label (text = 'Working As A Server'))
        print("Hellooooooooooo")


sm = ScreenManager()
sm.add_widget(MenuScreen(name='Menu'))
sm.add_widget(ServerScreen(name='Server'))
#sm.add_widget(ClientScreen(name='Client'))    

class FileApp(App):

    def build(self):

        #return Menu()

        return sm

if __name__ == '__main__':
    FileApp().run()

我犯错了哪一部分...请指出那部分&提供我如何解决...

输出--->
内部服务器屏幕
Hellooooooooooo

1 个答案:

答案 0 :(得分:2)

看看我的解决方案,受到来自恶劣的优秀评论的启发:Kivy: Changing screens in screen manager with an on_press event

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen




class MenuScreen(Screen):
    def switching_function(*args):
        global sm
        sm.current = 'Server'

    def __init__(self, **kwargs):

        super(MenuScreen, self).__init__(**kwargs)


#         def Server(instance):
#             self.clear_widgets()
#             self.add_widget(Label ( text = 'Inside server function'))
#             server = ServerScreen()
#             #return server
#             #server.function()

        self.add_widget(Label ( text = 'What Type Of Service You Want...???'))

        button1 = Button(text = 'Server',size_hint = (None,None),pos = (0,0))
        self.add_widget(button1)
#         button1.bind(on_press = Server)
        button1.bind(on_press = self.switching_function)

        button2 = Button(text = 'Client',size_hint = (None,None),pos = (100,0))
        self.add_widget(button2)
        #button2.bind(on_press = Client)




class ServerScreen(Screen):

    def __init__(self, **kwargs):
        super(ServerScreen, self).__init__(**kwargs)
        print('Inside server screen')

        self.clear_widgets()
        self.add_widget(Label (text = 'Working As A Server'))
        print("Hellooooooooooo")


sm = ScreenManager()
sm.add_widget(MenuScreen(name='Menu'))
sm.add_widget(ServerScreen(name='Server'))
#sm.add_widget(ClientScreen(name='Client'))    

class FileApp(App):

    def build(self):

        #return Menu()

        return sm

问题出在<{1}}里面

server = ServerScreen()

它创建了一个新的ServerScreen,而不是当前实际显示的ServerScreen(所以

   def Server(instance):
        self.clear_widgets()
        self.add_widget(Label ( text = 'Inside server function'))
        server = ServerScreen()
        #return server
        #server.function()
显示

,但ScreenManager不知道此附加屏幕是否存在。