Python Kivy:使用Popup.open()
我有带有弹出窗口的代码,其中包含文本输入。示例中的表单代码正常工作,但使用TextInputPopup时,构造函数在使用Popup.open()后会出现问题(注释中有costructor的版本)Python Kivy:使用Popup.open(),python,kivy,kivy-language,Python,Kivy,Kivy Language,我有带有弹出窗口的代码,其中包含文本输入。示例中的表单代码正常工作,但使用TextInputPopup时,构造函数在使用Popup.open()后会出现问题(注释中有costructor的版本) 使用构造函数我必须修复什么 函数TextInputPopup.next_step()中使用了TextInputPopup中的参数。我可以在函数SaveAs.on_call_popup()中使用这个参数吗?哪个函数调用TextInputPopup?我该怎么做 例如: Builder.load_s
- 使用构造函数我必须修复什么
- 函数TextInputPopup.next_step()中使用了TextInputPopup中的参数。我可以在函数SaveAs.on_call_popup()中使用这个参数吗?哪个函数调用TextInputPopup?我该怎么做
Builder.load_string('''
<SaveAs>:
teinp: teinp
Button:
text:'Hi users'
on_release: root.on_call_popup()
TextInput:
id: teinp
text:'text input'
<TextInputPopup>:
answer: answer
title: root.title
size_hint: None, None
size: app.root.width/2, app.root.height/2
auto_dismis: False
BoxLayout:
orientation: 'vertical'
Label:
text: root.label
TextInput:
id: answer
text: ''
BoxLayout:
Button:
text: 'Cancel'
on_press: root.dismiss()
Button:
text: 'OK'
on_press: root.dismiss()
''')
class TextInputPopup(Popup):
title = StringProperty()
label = StringProperty()
answer = ObjectProperty()
'''
def __init__ (self,title, label):
self.set_description(title, label)
return
'''
def set_description(self,title, label):
self.title = title
self.label = label
return
def get_answer(self):
return self.answer.text
class SaveAs(BoxLayout):
teinp = ObjectProperty()
def on_call_popup(self):
self.poti = TextInputPopup()
# self.poti = TextInputPopup('File Manager', 'Name')
self.poti.open()
self.poti.set_description('File Manager', 'Name')
self.poti.bind(on_dismiss = self.next_step)
return
def next_step(self, obj):
a = self.poti.get_answer()
self.teinp.text = self.poti.get_answer()
return
class ExplorerApp(App):
def build(self):
return SaveAs()
if __name__ == '__main__':
ExplorerApp().run()
Builder.load\u字符串(“”)
:
teinp:teinp
按钮:
文本:'Hi users'
on_release:root.on_call_popup()
文本输入:
id:teinp
文本:'文本输入'
:
答复:答复:
标题:root.title
大小提示:无,无
大小:app.root.width/2,app.root.height/2
自动解雇:错误
盒子布局:
方向:“垂直”
标签:
文本:root.label
文本输入:
id:回答
文本:“”
盒子布局:
按钮:
文本:“取消”
按:root.discouse()
按钮:
文字:“OK”
按:root.discouse()
''')
类文本输入弹出窗口(弹出窗口):
title=StringProperty()
label=StringProperty()
answer=ObjectProperty()
'''
定义初始(自我、标题、标签):
自我设置描述(标题、标签)
返回
'''
def集合描述(自身、标题、标签):
self.title=标题
self.label=标签
返回
def获取_答案(自我):
返回self.answer.text
类另存为(BoxLayout):
teinp=ObjectProperty()
def on_call_弹出窗口(自):
self.poti=TextInputPopup()
#self.poti=TextInputPopup('文件管理器','名称')
self.poti.open()
self.poti.set\u说明('文件管理器','名称')
self.poti.bind(on\u disclose=self.next\u步骤)
返回
def下一步(自身、obj):
a=self.poti.get_-answer()
self.teinp.text=self.poti.get_-answer()
返回
ClassExplorerApp(应用程序):
def生成(自):
返回SaveAs()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
ExplorerApp().run()
您必须调用父类的初始值设定项(\uuuu init\uuuu
),以便使用:
从kivy.app导入应用
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.popup导入弹出窗口
从kivy.properties导入StringProperty、ObjectProperty
从kivy.lang.builder导入生成器
Builder.load_字符串(“”)
:
teinp:teinp
按钮:
文本:'Hi users'
on_release:root.on_call_popup()
文本输入:
id:teinp
文本:'文本输入'
:
答复:答复:
标题:root.title
大小提示:无,无
大小:app.root.width/2,app.root.height/2
自动解雇:错误
盒子布局:
方向:“垂直”
标签:
文本:root.label
文本输入:
id:回答
文本:“”
盒子布局:
按钮:
文本:“取消”
按:root.discouse()
按钮:
文字:“OK”
按:root.discouse()
''')
类文本输入弹出窗口(弹出窗口):
title=StringProperty()
label=StringProperty()
answer=ObjectProperty()
定义初始(自我、标题、标签,**kwargs):
超级(文本输入弹出窗口,自我)。\uuuuu初始化(**kwargs)
自我设置描述(标题、标签)
def集合描述(自身、标题、标签):
self.title=标题
self.label=标签
def获取_答案(自我):
返回self.answer.text
类另存为(BoxLayout):
teinp=ObjectProperty()
def on_call_弹出窗口(自):
poti=TextInputPopup('文件管理器','名称')
poti.open()
poti.bind(on\u dismise=self.next\u步骤)
def下一步(自我,弹出):
self.teinp.text=popup.get_answer()
ClassExplorerApp(应用程序):
def生成(自):
返回SaveAs()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
ExplorerApp().run()
如果在派生类中使用\uuuuu init\uuuu
,则重新定义父构造函数,该构造函数通常会运行基类的初始化super
用于运行父类\uuuu init\uuuu
,无需显式调用父类,并允许您向其传递参数
原则上,无论何时在子类中覆盖
\uuuuuu init\uuuu
,都应该调用父类的\uuuuuu init\uuu
,并使用必要的参数进行正确初始化。@感谢您的快速建议。接下来请给我解释一下。函数“super”调用父类的构造函数,为什么我必须在这两个原因中都使用“super”?在TextInputPopup
类中,您需要\uuuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,因此,您需要显式调用基类的构造函数来正确初始化小部件。在您的SaveAs
类中,这是不必要的,因为您不需要初始值设定项,很抱歉,这是我的IDE自动完成的问题。。。我编辑了答案,添加了更多信息。@a)这是否意味着我必须在构造函数中为每个其父级具有构造函数的子级使用“super”?b) 你能在介绍性问题中为第二个问题添加评论吗(附示例)
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.properties import StringProperty, ObjectProperty
from kivy.lang.builder import Builder
Builder.load_string('''
<SaveAs>:
teinp: teinp
Button:
text:'Hi users'
on_release: root.on_call_popup()
TextInput:
id: teinp
text:'text input'
<TextInputPopup>:
answer: answer
title: root.title
size_hint: None, None
size: app.root.width/2, app.root.height/2
auto_dismis: False
BoxLayout:
orientation: 'vertical'
Label:
text: root.label
TextInput:
id: answer
text: ''
BoxLayout:
Button:
text: 'Cancel'
on_press: root.dismiss()
Button:
text: 'OK'
on_press: root.dismiss()
''')
class TextInputPopup(Popup):
title = StringProperty()
label = StringProperty()
answer = ObjectProperty()
def __init__(self, title, label, **kwargs):
super(TextInputPopup, self).__init__(**kwargs)
self.set_description(title, label)
def set_description(self, title, label):
self.title = title
self.label = label
def get_answer(self):
return self.answer.text
class SaveAs(BoxLayout):
teinp = ObjectProperty()
def on_call_popup(self):
poti = TextInputPopup('File Manager', 'Name')
poti.open()
poti.bind(on_dismiss=self.next_step)
def next_step(self, popup):
self.teinp.text = popup.get_answer()
class ExplorerApp(App):
def build(self):
return SaveAs()
if __name__ == '__main__':
ExplorerApp().run()