Python Forum
[Kivy] Create a function to store text fields and drop downs selection in KivyMD
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] Create a function to store text fields and drop downs selection in KivyMD
#1
Hi there everyone! Smile

I have been scratching my head for some time now Wall as I am trying to figure out a way to bind the button with a function to store all the values in separate variables for all the selections and entries made. I have tried to create a function to be called on release but I get all sorts of errors and seems like the function does not get recognised, therefore it shows not declared.

Besides that my question is what parameters do I need to pass to the function? and how do I retrive the values from other fucntions? Will return work to return for example to retrieve the value selected from the dropdown menu?

I am not familiar with object oriented therefore it would great if someone could point me in the right direction!

from kivy.core.window import Window
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.screen import Screen
from kivymd.uix.list import OneLineIconListItem
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

car_make = ["BMW", "Ford", "Nissan"]
KV = '''
<IconListItem>

    IconLeftWidget:
        icon: root.icon

MDScreen

    MDTextField:
        hint_text: "Model"
        mode: "rectangle"
        max_text_length: "6"
        icon_right: "clipboard-text-outline"
        icon_right_color: app.theme_cls.primary_color
        pos_hint:{'center_x': .5, 'center_y': .7}
        size_hint_x:None
        width:500
        font_size: "50"

    MDTextField:
        id: make
        mode: "rectangle"
        pos_hint: {'center_x': .5, 'center_y': .6}
        size_hint_x: None
        width: "500"
        font_size: "50"
        hint_text: "Make"
        helper_text: "Select make"
        icon_right: "Destination"
        icon_right_color: app.theme_cls.primary_color
        on_focus: if self.focus: app.make_menu.open()

    MDRectangleFlatIconButton:
        text: "Save"
        icon: "account-arrow-right"
        text_color: app.theme_cls.primary_color
        line_color: app.theme_cls.primary_color
        pos_hint: {"center_x": .5, "center_y": .2}
        font_size: "30"
        on_release: printvalue()
'''

class IconListItem(OneLineIconListItem):
    icon = StringProperty()

class Test(MDApp):
    Window.size = (1920, 1080)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)
        make_menu_items = [
            {
                "viewclass": "IconListItem",
                "icon": "",
                "height": dp(56),
                "text": f"{i}",
                "on_release": lambda x=f"{i}": self.set_make_item(x),
            } for i in car_make]
        
        self.make_menu = MDDropdownMenu(
            caller=self.screen.ids.make,
            items= make_menu_items,
            position="bottom",
            width_mult=4,
            max_height=dp(224)
        )

    def set_make_item(self, selected_item):
        self.screen.ids.make.text = selected_item
        self.make_menu.dismiss()
        selection = selected_item
        print(selection)
        return selection

    def build(self):
        self.theme_cls.theme_style="Light"
        self.theme_cls.primary_palette = "Green"
        return self.screen
    
    def storevalue(selection):
        print(selection)

    
Test().run()    
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class function does not create command button Heyjoe 2 2,294 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Updating box with Drop Down selection RCC99 3 2,557 May-13-2020, 09:34 AM
Last Post: RCC99
  [PyGUI] How to store continous output into a text file? Saraswathy 1 2,485 Aug-24-2018, 12:46 PM
Last Post: DeaD_EyE
  [WxPython] How to create a static white box text giu88 2 2,529 Aug-14-2018, 10:50 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020