【问题标题】:I want to use inheritance from Main and Second classes in Manager class but I get <<error : ScreenManager accepts only Screen widget>> ...please我想在 Manager 类中使用 Main 和 Second 类的继承,但我得到 <<error : ScreenManager 只接受 Screen 小部件>> ...请
【发布时间】:2020-02-22 10:29:06
【问题描述】:
class Main(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.name = "main"
        self.cols = 1
        self.Main_firstISS = Button(size_hint = (0.45,0.2) , pos_hint = {'center_x': 0.5 , 'y': 0.2})
        self.Main_secISS = Button(size_hint = (0.2,0.1) , pos_hint = {'x':0.8, 'y': 0} , text = "Exit")
        self.Main_thirdISS = Label(text = "Wellcome to Guess Game\n         play and enjoy")

    self.add_widget(self.Main_firstISS)
    self.add_widget(self.Main_secISS)
    self.add_widget(self.Main_thirdISS)

class Second(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.name = "second"
        self.cols = 1
        self.Second_firstISS = TextInput(hint_text = "What's your guess" , size_hint = (0.2,0.05) , pos_hint = {'center_x':0.5 , 'y': 0.7})
        self.Second_secISS = Button(size_hint = (0.1,0.1) , pos_hint = {'x':0.9 , 'y': 0})
        self.Second_firstISS.bind(on_press=self.callback)

    self.add_widget(self.Second_firstISS)
    self.add_widget(self.Second_secISS)

def callback(self , instance):
    print(f"{self.Second_firstISS.text}")

class Manager(ScreenManager , Main , Second):
    def __init__(self , **kwargs):
        super().__init__(**kwargs)
        my_main_page = Main()
        my_second_page = Second()
        my_main_page.__init__()
        my_second_page.__init__()

【问题讨论】:

    标签: python python-3.x kivy


    【解决方案1】:

    一开始你所有的 init 方法都是错误的。还有几个错误。您可以先尝试修复 init 函数,如果没有帮助,您可以尝试从 init 方法中删除所有内容,如下所示:

    class Main(Screen):
        # if you want to redefine init function, you should do it like this
        def __init__(self, **kwargs):
            super(Main, self).__init__(**kwargs)
    
        # screen is using relative layout, it doesn't have cols and rows!
        # self.cols = 1
        self.Main_firstISS = Button(size_hint = (0.45,0.2) , pos_hint = {'center_x': 0.5 , 'y': 0.2})
        self.Main_secISS = Button(size_hint = (0.2,0.1) , pos_hint = {'x':0.8, 'y': 0} , text = "Exit")
        self.Main_thirdISS = Label(text = "Wellcome to Guess Game\n         play and enjoy")
        self.add_widget(self.Main_firstISS)
        self.add_widget(self.Main_secISS)
        self.add_widget(self.Main_thirdISS)
    
    class Second(Screen):
        # same here
        def __init__(self, **kwargs):
            super(Second, self).__init__(**kwargs)
    
        self.Second_firstISS = TextInput(hint_text = "What's your guess" , size_hint = (0.2,0.05) , pos_hint = {'center_x':0.5 , 'y': 0.7})
        self.Second_secISS = Button(size_hint = (0.1,0.1) , pos_hint = {'x':0.9 , 'y': 0})
        self.Second_firstISS.bind(on_press=self.callback)
        self.add_widget(self.Second_firstISS)
        self.add_widget(self.Second_secISS)
    
        # put that function inside the class!
        def callback(self, *args):
            print(f"{self.Second_firstISS.text}")
    
    # you don't need to inherit Main and Second classes
    class Manager(ScreenManager):
        def __init__(self , **kwargs):
            # and here
            super(Manager, self).__init__(**kwargs)
    
        my_main_page = Main(name='main')
        my_second_page = Second(name='second')
        self.add_widget(my_main_page)
        self.add_widget(my_second_page)
    

    【讨论】:

      【解决方案2】:

      试试

      class Main(Screen):
          def __init__(self, **kwargs):
              super().__init__(**kwargs)
              self.name = "main"
              self.cols = 1
              self.Main_firstISS = Button(size_hint = (0.45,0.2) , pos_hint = {'center_x': 0.5 , 'y': 0.2})
              self.Main_secISS = Button(size_hint = (0.2,0.1) , pos_hint = {'x':0.8, 'y': 0} , text = "Exit")
              self.Main_thirdISS = Label(text = "Wellcome to Guess Game\n         play and enjoy")
      
              self.add_widget(self.Main_firstISS)
              self.add_widget(self.Main_secISS)
              self.add_widget(self.Main_thirdISS)
      class Second(Screen):
          def __init__(self, **kwargs):
              super().__init__(**kwargs)
              self.name = "second"
              self.cols = 1
              self.Second_firstISS = TextInput(hint_text = "What's your guess" , size_hint = (0.2,0.05) , pos_hint = {'center_x':0.5 , 'y': 0.7})
              self.Second_secISS = Button(size_hint = (0.1,0.1) , pos_hint = {'x':0.9 , 'y': 0})
              self.Second_firstISS.bind(on_press=self.callback)
      
              self.add_widget(self.Second_firstISS)
              self.add_widget(self.Second_secISS)
      
          def callback(self , instance):
              print(f"{self.Second_firstISS.text}")
      
      class Manager(ScreenManager):
          def __init__(self , **kwargs):
              super().__init__(**kwargs)
              my_main_page = Main()
              my_second_page = Second()
              self.add_widget(my_main_page)
              self.add_widget(my_second_page)
      

      【讨论】:

      • 还是一样的错误
      • Traceback(最近一次调用最后):文件“G:\ON Works\maintest111.py”,第 58 行,在 Guessgame().run() 文件“C:\Users\ naser\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py”,第 829 行,运行 root = self.build() 文件“G:\ON Works\maintest111.py”,第 54 行,在构建中返回 Manager() 文件“G:\ON Works\maintest111.py”,第 44 行,在 init super().__init__(**kwargs) 文件“C:\Users \naser\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\screenmanager.py",第 968 行,在 init 中
      • super().__init__(**kwargs) 文件“C:\Users\naser\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\screenmanager.py ",第 968 行,在 init 中 super(ScreenManager, self).__init__(**kwargs) 文件“G:\ON Works\maintest111.py”,第 14 行,在 init super().__init__(**kwargs) 文件“G:\ON Works\maintest111.py”,第 35 行,在 init self.add_widget(self.Second_firstISS) 文件“C:\ Users\naser\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\screenmanager.py",第 979 行,在 add_widget 中
      • 'ScreenManager 只接受屏幕小部件。') kivy.uix.screenmanager.ScreenManagerException: ScreenManager 只接受屏幕小部件。
      • 您的代码缺少部分。没有它们,我无法解决您的问题..您必须发布minimal reproducible example..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      相关资源
      最近更新 更多