在PySimpleGUI中滚动不适用于下拉选择

2024-09-29 17:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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,但滚动没有向下移动。按向上键时出现相同问题。在


Tags: tokeyselftruedataindexiforder
1条回答
网友
1楼 · 发布于 2024-09-29 17:21:32

也许这会让你更接近你想要的东西

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

相关问题 更多 >

    热门问题