0

When I click one of the buttons, the popup that is displayed is unedited and completely lacks the information provided in the load_string. How can I get the popup class to correspond to the data provided in the load_string? How it looks: screenshot. It should have a title and textinput widget.

EDITED: changed "Popup_Up" to "Pop_Up" in the kv language, however the issue still remains.

from logic import grandtotal
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty  
from kivy.lang import Builder

class Text_Input(TextInput):
    pass

class Pop_Up(Popup):
    texti_id = ObjectProperty(None)
    pass


class ShoppersApp(App, BoxLayout):
    popup_cost = ObjectProperty(Pop_Up())
    popup_people = ObjectProperty(Pop_Up())
    def build(self):
        return self

Builder.load_string("""
<Text_Input>:
    size_hint: .6, .5

<Pop_Up>:
    title: "Total Cost: "


<ShoppersApp>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "Total Cost"
            on_release: root.popup_cost.open()
        Button:
            text: "People"
            on_release: root.popup_people.open()
        Label:
            text: "3"
""")

if __name__ == "__main__":
    ShoppersApp().run()
4

2 回答 2

0

只需在定义类之前调用​​ Builder。在我的机器上进行以下工作。基维 1.7.0

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty  
from kivy.lang import Builder

Builder.load_string("""
<Text_Input>:
    size_hint: .6, .5

<Pop_Up>:
    title: "Total Cost: "

<ShoppersApp>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "Total Cost"
            on_release: root.popup_cost.open()
        Button:
            text: "People"
            on_release: root.popup_people.open()
        Label:
            text: "3"
""")

class Text_Input(TextInput):
    pass

class Pop_Up(Popup):
    texti_id = ObjectProperty(None)
    pass

class ShoppersApp(App, BoxLayout):
    popup_cost = ObjectProperty(Pop_Up())
    popup_people = ObjectProperty(Pop_Up())
    def build(self):
        return self

if __name__ == "__main__":
    ShoppersApp().run()
于 2013-07-01T02:41:32.517 回答
0

您的 kv 语言部分定义了 a 的<Popup_Up>:行为方式,但调用了 python 类Pop_Up。我不知道这是否是您的示例或您的实际代码中的错字,但无论哪种方式,它们都需要使用相同的名称。

编辑:根据您修复一件事的错字编辑,我认为真正的问题是线条

popup_cost = ObjectProperty(Pop_Up())
popup_people = ObjectProperty(Pop_Up())

在加载构建器文件之前运行(并Pop_Up()初始化 s) 。它们不会被 kv 语言追溯修改,因此它们永远不会设置标题。

似乎只需将 load_string 调用移动到文件顶部就可以解决此问题,但我最初是通过更改 ShoppersApp 的 init 方法来动态创建弹出窗口,如下所示。这可能是一种更安全/更有用的制作方法,具体取决于您在做什么。

class ShoppersApp(App, BoxLayout):
    popup_cost = ObjectProperty(Pop_Up())
    popup_people = ObjectProperty(Pop_Up())
    def __init__(self,*args,**kwargs):
        super(ShoppersApp,self).__init__(*args,**kwargs)
        self.popup_cost = Pop_Up()
        self.popup_people = Pop_Up()
    def build(self):
        return self

此外,让 ShoppersApp 子类化 app 和 BoxLayout 让我感到不安。如果在文档或其他内容中建议,它可能很好,甚至是正确的,但拥有单独的 App 和 ShopperLayout 小部件可能更安全。

于 2013-06-30T21:29:35.287 回答