Прокрутка не работает для выпадающего выбора в PySimpleGUI - PullRequest
1 голос
/ 25 сентября 2019

Когда я использую клавиатуру для выбора выпадающего меню, прокрутка не перемещается вверх и вниз, поэтому я не могу увидеть, какая из них выбрана прямо сейчас.Как перемещать прокрутку вверх и вниз при нажатии клавиатуры вверх и вниз.Мой пример кода выглядит следующим образом:

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()

При первом запуске приложения я просто вижу три раскрывающихся списка, как, enter image description here

Я нажал клавишу со стрелкой вниззатем выбор уменьшается один за другим, вот так:

enter image description here

enter image description here

Но после выбора30, нажмите клавишу выбора вниз, чтобы перейти к следующему, например 40, 50 .. кроме прокрутки, поэтому я не могу увидеть, какой из них выбран сейчас.Есть ли способ переместить выделение вместе с прокруткой?

enter image description here

См. Четвертое изображение, здесь выделение перемещено на 40, но прокрутка не сдвинута вниз.Та же проблема с нажатой клавишей вверх.

1 Ответ

1 голос
/ 27 сентября 2019

Может быть, это немного приблизит вас к тому, что вы ищете

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()

...