代码之家  ›  专栏  ›  技术社区  ›  rahlf23

Kivy:弹出窗口只能有一个小部件作为内容

  •  3
  • rahlf23  · 技术社区  · 7 年前

    我在我的中使用弹出窗口时遇到问题。kv文件。我知道一个弹出窗口只能有一个小部件作为其内容,但是如果我只是作为一个孩子传递一个包含标签和按钮的GridLayout,这难道不可行吗?

    下面是我的Python代码:

    import kivy, LabelB
    from kivy.app import App
    from kivy.graphics import Color, Rectangle
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.tabbedpanel import TabbedPanel
    from kivy.properties import ObjectProperty, StringProperty
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
    
    Builder.load_file('main.kv')
    
    class CustomPopup(Popup):
        pass
    
    class MenuScreen(Screen):
    
        def open_popup(self):
            the_popup = CustomPopup()
            the_popup.open()
    
    class SurveyScreen(Screen):
        pass
    
    sm = ScreenManager(transition=FadeTransition())
    sm.add_widget(MenuScreen(name='menu'))
    sm.add_widget(SurveyScreen(name='survey'))
    
    class MainApp(App):
    
        def build(self):
            return sm
    
    if __name__ == '__main__':
        MainApp().run()
    

    这是我的。kv文件:

    <CustomPopup>:
        title: 'Terms of Service'
        size_hint: .5, .5
        auto_dismiss: False
        GridLayout:
            cols: 1
            Label:
                size_hint: .9, .9
                halign: 'center'
                valign: 'middle'
                text: 'Insert terms of service text here'
                text_size: self.width, None
            Button:
                text: 'Close'
                on_release: root.dismiss()
    
    <MenuScreen>:
    
        FloatLayout:
    
            canvas.before:
                Rectangle:
                    source: 'menu.png'
                    size: self.size
                    pos: self.pos
    
            Label:
                pos_hint: {'x': .7, 'y': .85}
                text_size: self.size
                font_name: 'Arial'
                font_size: 26
                text: 'Sample'
                bold: True
    
            Button:
                text: 'Take Survey'
                size_hint: .2, .1
                pos_hint: {'x': .15, 'y': .1}
                on_release:
                    root.manager.transition.duration = 0.5
                    root.manager.current = 'survey'
    
            Button:
                text: 'Terms of Service'
                size_hint: .2, .1
                pos_hint: {'x': .6-self.size_hint_x, 'y': .1}
                on_release: root.open_popup()
    
            Button:
                text: 'Quit'
                size_hint: .2, .1
                pos_hint: {'x': .85-self.size_hint_x, 'y': .1}
                on_release: app.stop()
    
    <SurveyScreen>:
    
        GridLayout:
            cols: 1
            padding: 20
            spacing: 10
    
            Label:
                text: 'WELCOME!'
                font_size: 20
    
            Label:
                text: 'Some boring text'
    

    错误如下:“弹出窗口只能有一个小部件作为内容”

    我错过了什么明显的东西吗?提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  4
  •   FJSevilla    7 年前

    是的,它应该像你说的那样工作,你的代码是正确的。

    问题是加载。kv文件重复。作为您的 App 调用子类 MainApp , main.kv 如果在同一目录中,则自动加载(文档: How to load kv ). 另一方面,您可以使用 Builder.load_file ('main.kv') .

    您必须删除对的呼叫 Builder 或重命名 主控制程序 / 主要的千伏 .

    如果您删除对的呼叫 Builder.load_file 您必须创建 ScreenManager 实例。kv负载。您可以执行以下操作:

    class MainApp (App):
    
         def build (self):
             sm = ScreenManager (transition = FadeTransition ())
             sm.add_widget (MenuScreen (name = 'menu'))
             sm.add_widget (SurveyScreen (name = 'survey'))
             return sm