Afflicted Afflicted - 5 months ago 95
Python Question

Issue with a Kivy Spinner not updating text value right away

I was running a quick little test to display the currently selected value of a spinner by printing the value to a TextInput.

When the spinner is clicked it opens the drop down like it should when I click on the Display All option it sets it as active like it should (meaning it's the currently selected value) and the drop down closes but it doesn't seem to register correctly because it won't enter the function until I open the drop down and click on that value again.

Any suggestions as to how fix this?

from kivy.uix.spinner import Spinner
from kivy.uix.widget import Widget
import kivy.event
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.core.window import Window
import kivy


def Display_All(s):
displayWindow.text = s
return None


def Pick(event):
val = fileMenu.text
if val == 'Display All':
Display_All(val)

Root = Widget(size = (890, 505))

displayWindow = TextInput(pos = (250, 30), size = (620, 470))

fileMenu = Spinner(text = 'File',
values = ('File', 'Display All', 'Print', 'Search Specific'),
size = (120, 44), pos = (0, 460))

fileMenu.bind(on_press = Pick)


Root.add_widget(displayWindow)
Root.add_widget(fileMenu)



class ExampleApp(App):
def build(self):
return Root


Window.size = (890, 505)
if __name__ == '__main__':
ExampleApp().run()

Answer

The behaviour is completely expected: on_press triggers at the time of clicking, not when the text property changes. Therefore, you should change Pick to something like:

def Pick(sender, val):
    if val == 'Display All':
        Display_All(val)

(note that val will already contain the text value, so no need to retrieve it explicitly). And bind Pick to the event with

fileMenu.bind(text = Pick)
Comments