当我单击其中一个按钮时,显示的弹出窗口未经编辑,完全缺少load_string中提供的信息。如何让弹出类与load_string中提供的数据相对应?看起来如何:screenshot。它应该有一个标题和textinput小部件。
EDITED:用kv语言将“Popup_Up”更改为“Pop_Up”,但问题仍然存在。
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()
答案 0 :(得分:0)
您的kv语言部分定义<Popup_Up>:
的行为方式,但python类称为Pop_Up
。我不知道这是你的例子或实际代码中的拼写错误,但无论哪种方式,他们都需要给出相同的名称。
编辑:根据您的拼写错误编辑修复一件事,我认为真正的问题是线条
popup_cost = ObjectProperty(Pop_Up())
popup_people = ObjectProperty(Pop_Up())
在加载构建器文件之前运行(并初始化Pop_Up()
)。它们没有被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小部件可能更安全。
答案 1 :(得分:0)
在定义类之前调用Builder。我的机器上有以下工作。 Kivy 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()