Home > Mobile >  Python3 Kivy Multiple DropDown Menus not working
Python3 Kivy Multiple DropDown Menus not working

Time:10-17

I am attempting to write an application in Kivy but have run into an issue. When I add a second dropdown menu (called NotesDropDown) both of the dropdown menus stop working.

Here is the main code:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.stacklayout import StackLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button

class FileDropDown(DropDown):
    def new(self):
        print('New')

    def open_file(self):
        print('Open')

    def save(self):
        print('Save')

    def save_as(self):
        print('Save As')

class NotesDropDown(DropDown):
    def judges(self):
        print('Judges')

    def clubs(self):
        print('Clubs')

    def locations(self):
        print('Locations')

class AgilityApp(App):
    def build(self):
        root_widget = StackLayout(orientation='lr-tb')

        fdd = FileDropDown()
        fbutton = Button(text='File', size_hint=(None, None),
            size=(60,35))
        fbutton.bind(on_release=fdd.open)
        root_widget.add_widget(fbutton)

        ndd = NotesDropDown()
        nbutton = Button(text='Notes', size_hint=(None, None),
            size=(80,35))
        nbutton.bind(on_release=ndd.open)
        root_widget.add_widget(nbutton)

        return root_widget

AgilityApp().run()

and the kv file is

<FileDropDown>:
    Button:
        id: new
        text: 'New'
        size_hint_y: None
        height: 35
        on_release: root.new()
    Button:
        id: open
        text: 'Open'
        size_hint_y: None
        height: 35
        on_release: root.open_file()
    Button:
        id: save
        text: 'Save'
        size_hint_y: None
        height: 35
        on_release: root.save()
    Button:
        id: save_as
        text: 'Save As'
        size_hint_y: None
        height: 35
        on_release: root.save_as()

<NotesDropDown>:
    Button:
        id: judges
        text: 'Judges'
        size_hint_y: None
        height: 35
        on_release: root.judges()
    Button:
        id: clubs
        text: 'Clubs'
        size_hint_y: None
        height: 35
        on_release: root.clubs()
    Button:
        id: locations
        text: 'Locations'
        size_hint_y: None
        height: 35
        on_release: root.locations()

I have tried moving the NotesDropDown further away from the FileDropDown, widening it, changing layouts to BoxLayout, GridLayout, and a few others all with no success.

I will say that I can get to work just fine in Tkinter but was hoping to use kivy so that I can make it into a mobile application also.

Thank you in advance!

CodePudding user response:

I suspect the issue is garbage collection. Since you are not holding a reference to the DropDowns, they are garbage collected and the Buttons then do nothing. Try saving references to the DropDowns like this:

class AgilityApp(App):
    def build(self):
        Builder.load_string(kv)
        root_widget = StackLayout(orientation='lr-tb')

        self.fdd = FileDropDown()  # save a reference avoid garbage collection
        fbutton = Button(text='File', size_hint=(None, None),
            size=(60,35))
        fbutton.bind(on_release=self.fdd.open)
        root_widget.add_widget(fbutton)

        self.ndd = NotesDropDown()  # save a reference avoid garbage collection
        nbutton = Button(text='Notes', size_hint=(None, None),
            size=(80,35))
        nbutton.bind(on_release=self.ndd.open)
        root_widget.add_widget(nbutton)

        return root_widget
  • Related