Kivy: Using Popup.open()



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

  • 使用构造函数必须修复什么?
  • 来自 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__

最新更新