2017kamb
2017kamb

Reputation: 242

Scroll not working for dropdown selection in PySimpleGUI

When I use keyboard for dropdown selection, scroll doesn't move up and down,so I am not able to see which one is selected right now. How to move scroll up and down when keyboard is pressed up and down. My sample code is as following:

import PySimpleGUI as sg

class GUI():
    def __init__(self):
        self.data = [(10), (20), (30), (40), (50), (60), (70), (80), (90), (100)]
        self.work_order_currrent_selection_index = 0

    def run(self):
        layout = [[sg.Listbox(values=self.data, size=(35, 3), enable_events=True, key='selected_key')]]
        # Create the Window
        self.testWindow = sg.Window('Test', return_keyboard_events=True).Layout(layout).Finalize()
        self.testWindow.Maximize()
        self.testWindow.Element('selected_key').Update(set_to_index=0) 
        # Event Loop to process "events"
        while True:
            event, values = self.testWindow.Read()
            if event in('Up:111', '16777235'):
                if(hasattr(self, 'testWindow')):
                    self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index - 1) % len(self.data)
                    self.testWindow.Element('selected_key').Update(set_to_index=self.work_order_currrent_selection_index) 
            elif event in ('Down:116',' 16777237'):
                if(hasattr(self, 'testWindow')):
                    self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index + 1) % len(self.data)
                    self.testWindow.Element('selected_key').Update(set_to_index=self.work_order_currrent_selection_index)  

        self.testWindow.Close()

if __name__ == '__main__':
    app = GUI()
    app.run()

When application launch first time I just able to see three dropdown as, enter image description here

I pressed down arrow key then selection go down one by one like that,

enter image description here

enter image description here

But after selection of 30, on the down key press selection move to next one like 40, 50.. except scrolling, so I am not able to see which one is selected now. Is there any way to move selection along with scrolling?

enter image description here

See the fourth image, here selection moved to 40 but scroll not moved down. Same issue with up key pressed.

Upvotes: 1

Views: 1306

Answers (1)

Mike from PSG
Mike from PSG

Reputation: 5754

Maybe this will get you a little closer to what you're looking for

import PySimpleGUI as sg

class GUI():
    def __init__(self):
        self.data = [(10), (20), (30), (40), (50), (60), (70), (80), (90), (100)]
        self.work_order_currrent_selection_index = 0

    def run(self):
        layout = [[sg.Listbox(values=self.data, size=(35, 3), enable_events=True, key='selected_key')]]
        # Create the Window
        self.testWindow = sg.Window('Test', layout, return_keyboard_events=True, finalize=True)
        # self.testWindow.Maximize()
        self.testWindow.Element('selected_key').Update(set_to_index=0)
        # Event Loop to process "events"
        while True:
            event, values = self.testWindow.Read()
            if event is None:
                break
            if event.startswith('Up'):
                self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index - 1) % len(self.data)
                self.testWindow['selected_key'].Update(set_to_index=self.work_order_currrent_selection_index,scroll_to_index=self.work_order_currrent_selection_index )
            elif event.startswith('Down'):
                self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index + 1) % len(self.data)
                self.testWindow['selected_key'].Update(set_to_index=self.work_order_currrent_selection_index, scroll_to_index=self.work_order_currrent_selection_index)

        self.testWindow.Close()

if __name__ == '__main__':
    app = GUI()
    app.run()

Upvotes: 2

Related Questions