Skip to content

MDDropdownMenu width while centered below MDTextField #1803

@NGFWeber

Description

@NGFWeber

Description of the Bug

Hello,

i think there is a problem with MDDropdownMenu width.
if the width is set, it is no longer centered below a text field.

Default:
always works.

if menu.width = tf.width:
not centered

if menu.width > tf.width:
not centered

if menu.width < tf.width:
it seems to work as long as the Window is not resized, if the windows is resized to a certain size, it is again not centered.

Code and Logs

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

KV = '''
MDScreen:
   md_bg_color: self.theme_cls.backgroundColor
   
   MDBoxLayout:
      id: screen
      orientation: "vertical"
      padding: "50dp"
      spacing: "10dp"


      MDTextField:
         id: tf1
         pos_hint: {'center_x': .5, 'center_y': .6}
         size_hint_x: None
         width: "400dp"
         on_focus: if self.focus: app.menu.open()

         MDTextFieldHintText:
            text: "MDDropdownMenu #1 Default"


      MDTextField:
         id: tf2
         mode: "filled"
         pos_hint: {'center_x': .5, 'center_y': .6}
         size_hint_x: None
         width: "400dp"
         on_focus: if self.focus: app.menu2.open()

         MDTextFieldHintText:
            text: "MDDropdownMenu #2 menu.width = tf.widt"


            
      MDTextField:
         id: tf3
         pos_hint: {'center_x': .5, 'center_y': .6}
         size_hint_x: None
         width: "400dp"
         on_focus: if self.focus: app.menu3.open()

         MDTextFieldHintText:
            text: "MDDropdownMenu #3 menu.width > tf.widt"


      MDTextField:
         id: tf4
         pos_hint: {'center_x': .5, 'center_y': .6}
         size_hint_x: None
         width: "400dp"
         on_focus: if self.focus: app.menu4.open()

         MDTextFieldHintText:
            text: "MDDropdownMenu #3 menu.width < tf.widt" 
            
      Widget:
                     
'''

#1 Changing Window Size cuts dropdown width in half?



class Example(MDApp):

   def __init__(self, **kwargs):
      super().__init__(**kwargs)
      self.screen = Builder.load_string(KV)
      
      menu_items = [
         {
            "text": f"Item {i}",
            "on_release": lambda x=f"Item {i}": self.set_item(x),
         } for i in range(5)
      ]
      
      self.menu = MDDropdownMenu(
         caller=self.screen.ids.tf1,
         items=menu_items,
         position="bottom"
      )
      
      self.menu2 = MDDropdownMenu(
         caller=self.screen.ids.tf2,
         items=menu_items,
         position="bottom",
         width=400
      )
      
      self.menu3 = MDDropdownMenu(
         caller=self.screen.ids.tf3,
         items=menu_items,
         position="bottom",
         width=600
      )
      
      self.menu4 = MDDropdownMenu(
         caller=self.screen.ids.tf4,
         items=menu_items,
         position="bottom",
         width=300
      )
      
   def set_item(self, text_item):
      self.screen.ids.field.text = text_item
      self.menu.dismiss()


   def build(self):
      return self.screen


Example().run()

Screenshots

Image Image

Versions

  • OS: Win11
  • Python: 3.12.4
  • Kivy: 2.3.1
  • KivyMD: 2.0.1.dev0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions