How to make a spinner scrollable and can one limit the number of values in the spinner?

363 views
Skip to first unread message

Stephen Kirby

unread,
Oct 3, 2022, 7:54:14 PM10/3/22
to kivy-...@googlegroups.com
Hi -
I have 2 spinners working, however one spinner has too many values to fit the screen.  I thought this would make the spinner scrollable by default but it is not.  How to make a spinner scrollable?
Also, ideally, I would like to display 5 values at a time in this scrollable spinner.  Is this doable?

The relevant .py so far:
# create LayoutClass
class SampBoxLayout(FloatLayout):
    # For Spinner defining spinner clicked function
    def spinner_clicked1(self, value):
        print("Country selected is " + value)
        with open("country.txt","w") as f:
            f.write(f""+value)
            f.close()
=======================================
The .kv:
<SampBoxLayout>:
    orientation: 'vertical'
    FloatLayout:
        size_hint: 1, .95
        Label:
            halign: 'center'
            text: 'User Inputs'
            size_hint_y: None
            height: 44
            pos_hint: {'x': .01, 'y': .95}
            font_name: "data/fonts/DejaVuSans.ttf"
            font_size: 50
 
    # creating Spinner #1 for area selection
        Spinner:
        # Assigning id
            id: spinner_id1
        # Callback
            on_text: root.spinner_clicked1(spinner_id1.text)
        # initial text on spinner
            text: "Country"
        # total values on spinner
values: ["Andorra","Antigua","Armenia","Australia","Bermuda","China","Cuba","Denmark","Ethiopia","France","Ghana","Antigua","Bermuda","Cuba","Denmark","Ethiopia","France","Ghana","Bermuda","Cuba","Denmark","Ethiopia","France","Ghana","France","Ghana","Bermuda","Cuba","Denmark","Ethiopia","France","Ghana"]
        # declaring the size of the spinner
        # and the position of it
            size_hint: None, None
            size: 500, 50
            pos_hint:{'center_x':.5, 'top': .85}

Thx for any clues here.

Elliot Garbus

unread,
Oct 3, 2022, 9:13:13 PM10/3/22
to kivy-...@googlegroups.com

The Spinner is scrollable by default.  Create a minimal runnable example that highlights your issue.

 

You can limit the maximum number of options by changing the max_height of the dropdown_cls.  See the example below:

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.spinner import Spinner

kv =
"""
AnchorLayout:
    LimitSpinner:
        size_hint: None, None
        size: 200, 48
        text: 'Value 0'
        values: [f'Values {x}' for x in range(50)]     
"""


class LimitSpinner(Spinner):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.dropdown_cls.max_height = 4 * 48  # number of options, times the height of each option


class SpinApp(App):
   
def build(self):
       
return Builder.load_string(kv)


SpinApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAKhKqCAOMo%2Bt9dCHVf1z%2B7G8p5gcGF2AYHA%3DX76Xfy5Gf9O0gw%40mail.gmail.com.

 

Stephen Kirby

unread,
Oct 4, 2022, 2:08:18 PM10/4/22
to kivy-...@googlegroups.com
Sorry - took a second to get back.  Thanks Elliot for that info.  When I ran your posted code, I get a spinner that says "Values 0".  When I click on it, the drop down shows only one value "Values 0".  As recommended I'm posting my code for reference: 

.py code:

# spinner app in kivy using .kv file
# to change the kivy default settings we use this module config
from kivy.config import Config
from kivy.uix.label import Label

# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)

# Program to Show how to create a switch
# import kivy module
import kivy

# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App

# this restricts the kivy version i.e.
# below this kivy version you cannot
# use the app or software
kivy.require('1.9.0')

from kivy.uix.spinner import Spinner
from kivy.uix.floatlayout import FloatLayout

# Here for providing color to the background
from kivy.core.window import Window


# create LayoutClass
class SampBoxLayout(FloatLayout):
    # For Spinner defining spinner clicked function
    def spinner_clicked1(self, value):
        print("Country selected is " + value)
        with open("country.txt","w") as f:
            f.write(f""+value)
            f.close()

# # Make an App by deriving from the App class
class SampleApp(App):
    def build(self):

        # Set the background color for the window
        Window.clearcolor = (0.555, 0.261, .888, 0.5)
        return SampBoxLayout()

# create object for the Appclass
root = SampleApp()
# run the class
root.run()            
=======================================

the .kv code (sample.kv):
# .kv file implementation of the .py file

# Creating the Layout i.e., root of the Layout class
As I mentioned earlier, the drop-down is showing countries to the bottom of the screen (i.e., Andorra,...,Ghana).  But I do not see a scrollbar allowing for viewing of the rest of the values.  (Note: I know there are repeat countries - I simply did a copy/paste so that there would be a _LOT_ of countries, forcing, what I thought, would trigger a scrollbar.

Thanks for any clues (for why I can't seem to get a scrollbar to appear).


    


echidne

unread,
Oct 4, 2022, 2:27:57 PM10/4/22
to Kivy users support
Hi,
I tested the code of Eliot and it s working perfectlyspinner.png

ElliotG

unread,
Oct 5, 2022, 1:09:12 AM10/5/22
to Kivy users support
I modified your kv code,  (and I'm not sure that was required) and everything worked fine.  If I spin the mouse wheel on the list, it scrolls, if I grab and drag and time is strolls.

Here is the modified kv code:
<SampBoxLayout>:
#    orientation: 'vertical'  # SampleBoxLayout is a FloatLayout
#    FloatLayout:
#    size_hint: 1, .95

    Label:
#        halign: 'center'

        text: 'User Inputs'
        size_hint_y: None
        height: 44
        pos_hint: { 'top': .95}
#        font_name: "data/fonts/DejaVuSans.ttf"
        font_size: 50

    Spinner:
        id: spinner_id1
        on_text: root.spinner_clicked1(self.text)  # changed to self.text
        text: "Country"

        values: ["Andorra","Antigua","Armenia","Australia","Bermuda","China","Cuba","Denmark","Ethiopia","France","Ghana","Antigua","Bermuda","Cuba","Denmark","Ethiopia","France","Ghana","Bermuda","Cuba","Denmark","Ethiopia","France","Ghana","France","Ghana","Bermuda","Cuba","Denmark","Ethiopia","France","Ghana"]
        size_hint: None, None
        size: 500, 50
        pos_hint:{'center_x':.5, 'top': .85}

Screenshot 2022-10-04 220017.png

Stephen Kirby

unread,
Oct 5, 2022, 3:03:30 PM10/5/22
to kivy-...@googlegroups.com
OK.  I will take a look and test.  Thx for all your inputs!

--Steve

Stephen Kirby

unread,
Oct 5, 2022, 3:32:09 PM10/5/22
to kivy-...@googlegroups.com
Your edits work perfectly.  I can now scroll!  Thanks again.

On Tue, Oct 4, 2022 at 11:09 PM ElliotG <elli...@cox.net> wrote:
Reply all
Reply to author
Forward
0 new messages