methods - Kivy Popup not able to access root method
问题描述
I am a newbie here so feel free to correct me if I am not following proper procedures.
I have a Kivy app that opens a popup. In the popup, I can enter 2 numbers then click the Add button which should add the 2 numbers. I get an error saying, "AttributeError: 'CustomPopup' object has no attribute 'addNum'"
Why would this be?
test.py file
import kivy
kivy.require('1.9.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
class CustomPopup(Popup):
pass
class MyStuff(BoxLayout):
num1 = StringProperty
num2 = StringProperty
answer = ''
def openPopup(self):
the_popup = CustomPopup()
the_popup.open()
def addNum(self):
self.answer = str(int(self.num1) + int(self.num2))
class MyStuffApp(App):
def build(self):
return MyStuff()
if __name__ == '__main__':
MyStuffApp().run()
mystuff.kv file
#: import main test
<MyStuff>:
orientation: 'vertical'
spacing: 5
padding: 5
Button:
text: 'Change numbers'
on_press: root.openPopup()
font_size: 50
Label:
text: root.answer
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: 'Addition'
num1: number2
num2: number2
BoxLayout:
orientation: 'vertical'
Label:
text: '1st number'
TextInput:
id: number1
Label
text: '2nd number'
TextInput
id: number2
Button:
text: 'Add'
on_press: root.addNum()
解决方案
首先,要访问addNum
,您必须app.root.addNum
从 kv 部分调用。您还必须发送要添加的值,即您在文本框中输入的文本:(number1.text, number2.text)
。所以运行代码可能是这样的:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
Builder.load_string("""
<MyStuff>:
orientation: 'vertical'
spacing: 5
padding: 5
Button:
text: 'Change numbers'
on_press: root.openPopup()
font_size: 50
Label:
text: root.answer
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: 'Addition'
num1: number2
num2: number2
BoxLayout:
orientation: 'vertical'
Label:
text: '1st number'
TextInput:
id: number1
Label
text: '2nd number'
TextInput
id: number2
Button:
text: 'Add'
on_press: app.root.addNum(number1.text, number2.text)
""")
class CustomPopup(Popup):
pass
class MyStuff(BoxLayout):
# num1 = StringProperty()
# num2 = StringProperty()
answer = ''
def openPopup(self):
the_popup = CustomPopup()
the_popup.open()
def addNum(self, *args):
# self.answer = str(int(self.num1) + int(self.num2))
self.answer = str(int(args[0]) + int(args[1]))
print(self.answer)
class MyStuffApp(App):
def build(self):
return MyStuff()
if __name__ == '__main__':
MyStuffApp().run()
推荐阅读
- linux - stderr 重定向非错误消息的问题
- django - Django REST 框架。如何创建两个具有不同值的相同标签?
- javascript - 检测网页上的用户输入(按键或输入的事件处理)
- python - 从 Python 中不可搜索的流中增量读取单项 ZIP 文件
- java - 如何通过常量从数据模型中获取数据
- centos - CentOS VM(由 Openstack 管理)添加辅助 IP 但辅助 IP 无法 ping 其他主机的 IP
- javascript - Sequelize:如何使用包含创建具有嵌套表关系的数据
- printing - C - 当我回答正确答案时,我的代码没有打印 [first] IF 语句 - 为什么?
- python - 无法转换类型的对象
张量 - f# - 是否可以在运行时获取联合的标签?