English 中文(简体)
MDDropD 项目动态杂质
原标题:MDDropDownItem Dynamic implemetnation

我的目标是减少一个相对下降的菜单和“+” but子,把“+”icon改为一个新的下降项目,并在此之后重新出现“+”icon,准备增加第三个减小项目。

I ve been on this for a while. I ve tried a custom widget but i might have gotten the code wrong.. I ve tried a method to build the MDDropDownItem and many more. The problem is always a KeyError: label_item . As it seems, a MDDropDownItem doesn t get inizialized with this property if it s created in the python file... this seems weird to me, so here I am!

其次,将产生错误的最简单方式放下一米。 如果有人能够解决这个问题,那将令人惊讶!

档案:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.relativelayout import RelativeLayout
from kivymd.uix.screen import MDScreen
from kivymd.uix.label import MDLabel
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDIconButton
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.dropdownitem import MDDropDownItem
from kivymd.uix.selectioncontrol import MDCheckbox
import sqlite3
import os

class ExampleApp(MDApp):
    def build(self):
        
        return Builder.load_file( example.kv )
    

    def set_dropmenutext(self, text):
        self.root.ids.types_dropdownmenu.text = text
        self.menu.dismiss()

    def open_menu(self, call):

        menu_items = [{"text": i, 
                       "viewclass": "OneLineListItem", 
                       "on_release": lambda x= i: self.set_dropmenutext(x)} for i in ["INTEGER", "REAL", "TEXT", "BLOB", "NULL"]]
        
        self.menu = MDDropdownMenu(
                        caller=call,
                        items=menu_items,
                        width_mult=4,
                        position="auto")
        
        self.menu.open()
        
    def add_column(self):
        
        grid = self.root.ids.column_grid
        add_btn = self.root.ids.add_col

        grid.remove_widget(add_btn)

        ddi = MDDropDownItem(text= "Select Type")

        grid.add_widget(ddi)


if __name__ ==  __main__ :  
    ExampleApp().run()

KV 档案:

MDBoxLayout:
    MDGridLayout:
        id: column_grid
        cols: 1

        RelativeLayout:

            MDDropDownItem:
                id: types_dropdownmenu
                text: "Select Type"
                font_size: 18
                size_hint: (0.3, 0.5)
                pos_hint: { center_x : 0.2,  center_y : 1}
                on_release: app.open_menu(self)

        MDIconButton:
            id: add_col
            icon:  plus-thick 
            theme_text_color:  Custom 
            text_color: (0.5, 0.5, 1, 1)
            user_font_size: "20sp"
            on_release: app.add_column()
    ```


Sorry for the formatting i guess. I never undertood how to make it work properly...




问题回答

问题是,在建立<条码>之前,你正在制定<条码>的<>文本。 标题 <代码>MDDropDownI中的触发代码,以在<代码>MDDropDownItem上设置text/ >。 该代码使用<代码>id (label_item)。

在建立<编码>ids之前,你可以推迟制定<>code>text。 这样做的一个途径是使用<代码>Clock.slate_once():

def add_column(self):
    grid = self.root.ids.column_grid
    add_btn = self.root.ids.add_col

    grid.remove_widget(add_btn)

    ddi = MDDropDownItem()  # do not set text here

    grid.add_widget(ddi)
    
    Clock.schedule_once(partial(self.setup_ddi, ddi))

def setup_ddi(self, ddi, _dt):
    # set the text here
    ddi.text = "Select Type"




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签