Give/get 个来自 "on_press" 的弹出窗口中的参数

Give/get arguments in popup from "on_press"

请原谅我的简单问题,但有些事情我不明白。 我想在一种方法中将位于弹出 window 中的按钮的一些参数提供给另一种方法。

示例:

.py代码

class GeneralForm(TabbedPanel):

    def EDIT(self,D):
        box1=BoxLayout(orientation='vertical')
        t1=TextInput(text=GeneralForm.PARSE(self,D)) 
        b2=Button(text='Save')
        b3=Button(text='Cancel')


        box2=BoxLayout()

        box2.add_widget(b2)
        box2.add_widget(b3)

        box1.add_widget(t1)
        box1.add_widget(box2)

        popup = Popup(content=box1,auto_dismiss=False,size_hint=(.75,.75),title='Edit')
        b2.bind(on_press=self.SAVE_EDIT) <====== There is a problem
        b3.bind(on_press=popup.dismiss) 
        popup.open()


    def SAVE_EDIT(self,instance):
       !!! DOING SOMETHING !!!

https://s3.amazonaws.com/xasan/snapshot/stack1.png

我想要的:

在方法 "EDIT" 中,我有文本输入 "t1"。更改此文本输入中的文本后,我按下按钮 "b2",它调用带有两个参数的方法 SAVE_EDIT。

所以,我想给 "SAVE_EDIT" 方法提供第三个参数,该方法将 return 在 t1 中编辑文本。

像这样:

.py代码

class GeneralForm(TabbedPanel):

def EDIT(self,D):
    box1=BoxLayout(orientation='vertical')
    t1=TextInput(text=GeneralForm.PARSE(self,D))
    b2=Button(text='Save')
    b3=Button(text='Cancel')


    box2=BoxLayout()

    box2.add_widget(b2)
    box2.add_widget(b3)

    box1.add_widget(t1)
    box1.add_widget(box2)

    popup = Popup(content=box1,auto_dismiss=False,size_hint=(.75,.75),title='Edit')
    b2.bind(on_press=self.SAVE_EDIT(t1.txt)) <====== There is a problem
    b3.bind(on_press=popup.dismiss) 
    popup.open()


def SAVE_EDIT(self,instance,TEXT): <====== There is a problem
   !!! DOING SOMETHING with TEXT!!!

一步一步:

  1. Popen window was opened with some text in the text input.
  2. We edited text, deleted something or added.
  3. We are clicking on button "Save"(b2) and all text in txt input(t1) push to method "SAVE_EDIT" where we save,parse or do something else with this text.

提前致谢。

您可以使用 lambda:

on_press=lambda instance, text=t1.txt: self.SAVE_EDIT(instance, TEXT=text)

functools.partial():

on_press=partial(self.SAVE_EDIT, TEXT=t1.txt)

两种变体在 bind 调用时都使用 t1.txt,即,当您按下按钮时,该值可能已过时。

要使用当前最新值:

on_press=lambda instance: self.SAVE_EDIT(instance, TEXT=t1.txt)

在这种情况下,每次调用回调时都会调用 t1.txt