python - 滚动不适用于 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 但滚动没有向下移动。按下向上键时同样的问题。
解决方案
也许这会让你更接近你正在寻找的东西
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()
推荐阅读
- google-apps-script - 是否可以只排序一个范围而不是整行?
- r - 如何使用 Rstudio 绘制 EKMA 图
- java - 列出不同的对象,这怎么可能?
- javascript - jquery文件浏览添加和删除但不能从其他输入标签中获取文件,除了第一个
- flutter - 颤振改造@Multipart注释的问题
- php - 动态 url(由斜杠分隔)参数获取数组?(例如:/category,cat1,cat2/types,type1,type2)
- android - 使用选择器更改时,浮动操作按钮图标未居中对齐
- javascript - 布局抖动会导致不正确的绘画吗?
- javascript - 如何从对象键数组中计算剩余值?
- python - 如何打开特定文件,并在下一行写入数据,而不是删除以前的数据?