本文介绍了Kivy:弹出式窗口只能包含一个小组件作为内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.kv文件中使用弹出窗口时遇到问题.我知道弹出窗口的内容只能包含一个小部件,但是,如果我仅将GridLayout作为包含Label和Button的子级传递,这行不通吗?

I am having an issue with using a popup in my .kv file. I understand that a popup can only have one widget as it's content, however if I am only passing a GridLayout as a child that includes a Label and Button, shouldn't this work?

这是我的Python代码:

Here is my Python code:

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'

错误如下:弹出式窗口只能将一个小部件作为内容"

The error is as follows: 'Popup can have only one widget as content'

我在这里缺少明显的东西吗?预先感谢.

Am I missing something obvious here? Thanks in advance.

推荐答案

是的,它应该如您所说的那样工作,您的代码是正确的.

Yes, it should work as you say, your code is correct.

问题是.kv文件的加载重复.由于您的App子类称为MainApp,因此main.kv如果位于同一目录中,则会自动加载(文档:如何加载kv ).另一方面,您可以使用Builder.load_file ('main.kv')明确上传文件.

The problem is that the loading of the .kv file is duplicated. As your App subclass is called MainApp, main.kv is loaded automatically if it is in same directory (Doc: How to load kv). On the other hand, you explicitly upload the file using Builder.load_file ('main.kv').

您必须删除对Builder的呼叫或重命名MainApp/main.kv.

You must remove the call to Builder or rename MainApp/main.kv.

如果删除对Builder.load_file的调用,则在加载.kv后必须创建ScreenManager实例.您可以执行以下操作:

If you delete the call to Builder.load_file you must create the ScreenManager instance once the .kv is loaded. You can do something like:

class MainApp (App):

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

这篇关于Kivy:弹出式窗口只能包含一个小组件作为内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:53