首页 > 解决方案 > 滚动不适用于 PySimpleGUI 中的下拉选择

问题描述

当我使用键盘进行下拉选择时,滚动不会上下移动,所以我现在无法看到选择了哪一个。上下按下键盘时如何上下移动滚动。我的示例代码如下:

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

当应用程序第一次启动时,我只能看到三个下拉列表, 在此处输入图像描述

我按下箭头键然后选择像这样一个一个地向下,

在此处输入图像描述

在此处输入图像描述

但是在选择 30 之后,在向下键上按下选择移动到下一个,比如 40、50 .. 除了滚动,所以我现在看不到哪个被选中了。有没有办法随着滚动移动选择?

在此处输入图像描述

看第四张图片,这里选择移动到 40 但滚动没有向下移动。按下向上键时同样的问题。

标签: pythonpython-3.xpysimplegui

解决方案


也许这会让你更接近你正在寻找的东西

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


推荐阅读