Kivy,提交按钮时调用下拉列表

2024-04-25 23:41:54 发布

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

我正在尝试创建一个下拉列表,在提交按钮时调用该列表。调用下拉列表后,我希望将按钮的值设置为下拉列表中已选择的按钮。然后我想在代码中检索这个值,以执行一些逻辑。我发现这个问题之前已经被问过了,它准确地概括了我想要实现的目标。但是,我尝试将答案合并到代码中,但由于某些原因,下拉列表没有出现。如果有人能帮我,告诉我我现在在做什么,我将不胜感激

报废.py

from kivy.app import App
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.dropdown import DropDown
import datetime as dt

Window.size = (800,600)

class CustomDropDown(DropDown):

    def __init__(self, **kwargs):
        super(CustomDropDown, self).__init__(**kwargs)
        self.add_buttons()

    def add_buttons(self):
        for index in range(10):

            btn = Button(text='Value %d' % index, size_hint_y=None, height=44)

            btn.bind(on_release=lambda btn: self.select(btn.text))

            self.add_widget(btn)

class MainWindow(Screen):

    check_solid = ObjectProperty(False)
    check_water = ObjectProperty(False)
    check_boiling = ObjectProperty(False)
    s_id = str(dt.datetime.now().strftime("%y%m%d%H%M"))

    def btn(self):

        print(self.check_solid.state)
        print(self.check_water.state)
        print(self.check_boiling.state)

class MyScreenManager(ScreenManager):
    def Run_Draws_Test(self, value):
        print(value)

class Checkbox(CheckBox):
    pass


class ScrapApp(App):
    title = "Chemistry Sample Requests"

    def build(self):
        return MyScreenManager()


if __name__ == '__main__':
    ScrapApp().run()

报废.kv

#:import Factory kivy.factory.Factory

<Button>:
    size_hint: 0.1,0.1

<Label>:
    size_hint: 0.1,0.1

<Checkbox>:
    size_hint: 0.1,0.1

<TextInput>:
    size_hint: 0.2,0.1
    multiline: False

<CustomDropDown>:
    on_select:
        app.root.ids.MainWindow.ids.mainbutton.text = '{}'.format(args[1])
        app.root.Run_Draws_Test(args[1])

<MainWindow>:
    orientation: "vertical"

    check_solid: solid_check
    check_boiling: boiling_check
    check_water: water_check

    FloatLayout:

        Label:
            text: "Sample ID: "
            pos_hint: {"x":0.05, "top":0.95}

        Label:
            text: root.s_id
            pos_hint: {"x":0.2, "top": 0.95}

        Label:
            text: "Plant ID: "
            pos_hint: {"x":0.05, "top": 0.8}

        Button:
            id: mainbutton
            text: "Choose"
            pos: 400,400
            size_hint: None,None
            size: 150, 50
            on_release: Factory.CustomDropDown().open(self)

        Label:
            text: "Formulation: "
            pos_hint: {"x":0.05, "top": 0.65}

        TextInput:
            id: id_formulation
            pos_hint: {"x":0.2, "top": 0.65}

        Label:
            text: "Solids Test: "
            pos_hint: {"x":0.05, "top": 0.5}

        Checkbox:
            id: solid_check
            pos_hint: {"x":0.25, "top": 0.5}

        Label:
            text: "Water Content Test: "
            pos_hint: {"x":0.05, "top": 0.35}

        Checkbox:
            id: water_check
            pos_hint: {"x":0.25, "top": 0.35}

        Label:
            text: "Boiling Point: "
            pos_hint: {"x":0.05, "top": 0.2}

        Checkbox:
            id: boiling_check
            pos_hint: {"x":0.25, "top": 0.2}

        Button:
            text: "Submit"
            pos_hint: {"x": 0.7, "top": 0.5}
            on_release: root.btn()

<MyScreenManager>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 0.5
        Rectangle:
            pos: 0,0
            size: 800, 600
    MainWindow:
        id: MainWindow
        name: 'MainWindow'

Tags: textfromposimportselfid列表size
1条回答
网友
1楼 · 发布于 2024-04-25 23:41:54

我以前遇到过这样的虫子。我不知道Kivy是否能按预期工作,或者如何修复它。任何一个比我更有知识的人,我很高兴听到这个道理

class CustomDropDown(DropDown):

    def __init__(self, **kwargs):
        super(CustomDropDown, self).__init__(**kwargs)
        self.add_buttons()

    def add_buttons(self):
        for index in range(10):

            #btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
            btn = Button(text='Value %d' % index)
            btn.size_hint_y = None
            btn.height = 44


            btn.bind(on_release=lambda btn: self.select(btn.text))

            self.add_widget(btn)

这些事情只会让我感到沮丧

相关问题 更多 >