基维:使用弹出窗口。打开()

2024-10-04 05:32:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有Popup的代码,其中包含TextInput。示例中的表单代码是有效的,但是使用TextInputPopup构造函数在使用后会下降弹出窗口。打开()(注释中有构造器的版本)。在

  • 使用构造函数必须修复什么?在
  • TextInputPopup中的参数用于函数TextInputPopup.next_步骤()。我能在函数中使用这个参数吗SaveAs.on_call_弹出窗口(),它调用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()

Tags: textanswerselfreturntitleondefroot
1条回答
网友
1楼 · 发布于 2024-10-04 05:32:32

必须调用父类的初始值设定项(__init__),才能使用^{}

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__,并使用必要的参数正确初始化。在

相关问题 更多 >