Kivy-按下按钮时打印下拉列表文本

时间:2019-01-21 22:11:29

标签: python kivy

当我按下下面代码的“更新”按钮时,我想打印下拉列表文本。

例如: -当我选择“值1”时,将自动打印“值1”。 -当我按“更新”时,每次都会打印“值9”。 -我想要的预期结果是,当按下“更新”按钮时打印“值1”;

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

actual_value = '0'

class Test(App):

    def build(self):
        box = BoxLayout(orientation = 'vertical')
        btn1 = Button(text='Update', font_size = 30, size_hint_y=0.15, on_release = self.update)
        label = Label(text = 'LABEL')
        button = Button(text='Selecione', font_size=30, size_hint_y=0.15 , on_release = self.lista)
        box.add_widget(btn1)
        box.add_widget(label)
        box.add_widget(button)

        self.dropdown = DropDown()
        self.dropdown.bind(on_select = lambda instance, x: setattr(button, 'text' , x))

        for index in range(10):
            global actual_value
            btn = Button(text = 'Value %d' %index , size_hint_y = None, height = 44 , on_release = lambda btn: print(btn.text))
            btn.text = 'Value %d' % index
            btn.bind(on_release = lambda btn: self.dropdown.select(btn.text))
            self.dropdown.add_widget(btn)

        actual_value = btn.text

        return box

    def lista(self, button):
        self.dropdown.open(button)

    def update(self, btn1):
        print(actual_value)

Test().run()

0 个答案:

没有答案