如何在Kivymd中的按钮下方找到MDDropdownMenu?

时间:2020-07-16 16:20:13

标签: python python-3.x kivy kivy-language

我在主屏幕上创建了三个MDDropdownMenu,但无法正确放置它们。我希望MDDropdownMenu在其调用者按钮下直接打开,我也给了它位置和ver_growth和hor_growtn,但是我无法正确放置它。它默认情况下处于其位置,并将其自身定位在屏幕外,如下图所示。由于我的应用程序已经有很多屏幕并且构建已经结束,所以我不想更改代码的布局。请帮助我如何正确放置它。预先感谢。

我的main.py文件

from kivymd.app import MDApp
import json
from datetime import datetime
from kivy.lang import Builder
from kivy.uix.dropdown import DropDown
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivymd.uix.button import  MDFlatButton ,MDRectangleFlatButton, MDRaisedButton, MDFillRoundFlatButton, MDFillRoundFlatIconButton, MDRoundFlatIconButton ,MDFlatButton, MDIconButton, MDRectangleFlatIconButton
from kivymd.uix.label import MDLabel, MDIcon
from kivymd.uix.textfield import MDTextField
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem
from kivy.core.window import Window
from kivymd.uix.menu import MDDropdownMenu, MDMenuItem 


class RootWidget(ScreenManager):
    pass

class HomeScreen(Screen):
    
    def on_kv_post(self, base_widget):
        weightbutton = self.ids.packageweight_button
        sizebutton = self.ids.packagesize_button
        typebutton= self.ids.packagetype_button
        weightdropdown_item=[{"viewclass": "MDMenuItem", "text": "0-5 kgs"},
        {"viewclass": "MDMenuItem", "text": "5-10 kgs"},
        {"viewclass": "MDMenuItem", "text": "10-15 kgs"},
        {"viewclass": "MDMenuItem", "text": "15-20 kgs"}]
        self.weightdropdown = MDDropdownMenu(caller=weightbutton,position="bottom",ver_growth= "up",hor_growth= "right", width_mult=4, max_height=250, items=weightdropdown_item, callback= self.dropdown_callback)
        sizedropdown_item=[{"viewclass": "MDMenuItem", "text": "Small"},
        {"viewclass": "MDMenuItem", "text": "Medium"},
        {"viewclass": "MDMenuItem", "text": "Large"}]
        self.sizedropdown = MDDropdownMenu(caller=sizebutton,position="bottom",ver_growth= "up",hor_growth= "right", width_mult=4, max_height=250, items=sizedropdown_item, callback= self.menu_callback)
        
        typedropdown_item=[{"viewclass": "MDMenuItem", "text": "Electronic"},
        {"viewclass": "MDMenuItem", "text": "Stationary"},
        {"viewclass": "MDMenuItem", "text": "Food items"},
        self.typedropdown = MDDropdownMenu(caller=typebutton,position="bottom",ver_growth= "up",hor_growth= "right", width_mult=4, max_height=350,  items=typedropdown_item,callback= self.ddmenu_callback)
    
    def weightdrop(self):
        self.weightdropdown.open()

    def sizedrop(self):
        self.sizedropdown.open()

    def typedrop(self):
        self.typedropdown.open()

    def dropdown_callback(self, instance):
        self.weightdropdown.dismiss()

    def menu_callback(self, instance):
        self.sizedropdown.dismiss()
    
    def ddmenu_callback(self, instance):
        self.typedropdown.dismiss()


class MainApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette= "Green"
        return RootWidget()
    
if __name__ == "__main__":
    MainApp().run() 

我的design.kv文件

<HomeScreen>:
    NavigationLayout:
        ScreenManager:
            Screen:
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "Pick It Up"
                        elevation: 10
                        left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]

                    Widget:
    
                GridLayout:
                    cols: 1
                    padding: 20, 20
                    spacing: 10,10
                    size_hint: 1,0.87
                    MDRectangleFlatIconButton:
                        id: packageweight_button
                        icon: 'weight-kilogram'
                        text: "Package weight"
                        size_hint: (1,1)
                        on_release: root.weightdrop()                     
                    MDRectangleFlatIconButton:
                        id: packagesize_button
                        icon: 'move-resize'
                        text: "Package Size"
                        size_hint: (1,1)
                        on_press: root.sizedrop()
                    MDRectangleFlatIconButton:
                        id: packagetype_button
                        icon: 'format-list-bulleted-type'
                        text: "Package Type"
                        size_hint: (1,1)
                        on_press: root.typedrop()
                    MDRectangleFlatIconButton:
                        icon: 'map-marker'
                        text: "Pickup and Drop Lacations"
                        size_hint: (1,1)
                        on_press: root.pickup_drop_loc()
                    MDLabel:
                        id: packageweight
                        text: ""
                    MDLabel:
                        id: packagesize
                        text: ""
                    MDLabel:
                        id: packagetype
                        text: ""
                    MDLabel:
                        id: pickuploc
                        text: ""
                    MDLabel:
                        id: droploc
                        text: ""
                    RelativeLayout:
                        MDFillRoundFlatIconButton:
                            icon: 'cash-usd'
                            text: "Check total amount"
                            text_color: 1,1,1,1
                            size_hint: (1,1)
                            on_press: root.total_amount()
        MDNavigationDrawer:
            id: nav_drawer
            BoxLayout:
                orientation: 'vertical'
                padding: 20,20
                spacing: 30,30
                Image:
                    source: 'male-avatar-profile-picture-vector-10211761.jpg'
                MDLabel:
                    text:"User-Name"
                    font_style: 'H6'
                    size_hint_y: None
                    height: self.texture_size[1]
                MDLabel:
                    text: "User-email-id"
                    
                    font_style: 'Caption'
                    size_hint_y: None
                    height: self.texture_size[1]
                ScrollView:
                    MDList:
                        OneLineIconListItem:
                            text: "Profile"
                            IconLeftWidget:
                                icon: 'face-profile'
                        OneLineIconListItem:
                            text: "Previous orders"
                            IconLeftWidget:
                                icon: 'page-previous-outline'
                        OneLineIconListItem:
                            text: "Logout"
                            on_press:                                 
                                root.logout()
                                nav_drawer.set_state("close")
                            IconLeftWidget:
                                icon: 'logout'
    

<RootWidget>:
    HomeScreen:
        name: "home_screen"   

在图像中,下拉菜单位于应用程序屏幕之外:

enter image description here

0 个答案:

没有答案