gpt4 book ai didi

python - Kivy:使用 Popup.open()

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:51 42 4
gpt4 key购买 nike

我有带有Popup的代码,其中包含TextInput。示例中的表单代码可以正常工作,但是使用 TextInputPopup 构造函数在使用 Popup.open() 后会崩溃(带构造函数的版本在注释中)。

  • 使用构造函数必须修复哪些内容?
  • 来自TextInputPopup的参数在函数TextInputPopup.next_step()中使用。我可以在调用 TextInputPopup 的函数 SaveAs.on_call_popup() 中使用此参数吗?我该怎么办?

示例:

   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()

最佳答案

您必须调用父类的初始化程序 (__init__),为此用途 super :

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()

如果在派生类中使用__init__,则将重新定义父构造函数,该构造函数通常会运行基类的初始化。 super 用于运行父类 __init__,无需显式调用父类,并允许您向其传递参数。

原则上,每当您在子类中覆盖 __init__ 时,您都应该使用正确初始化所需的参数调用父类的 __init__

关于python - Kivy:使用 Popup.open(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259400/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com