pysimplegui不同元素如何同步滚动
时间: 2023-09-18 21:09:17 浏览: 267
在PySimpleGUI中,不同元素之间的滚动同步可以通过使用`TKScrollableFrame`实现。`TKScrollableFrame`是一个自定义的PySimpleGUI元素,它可以将多个PySimpleGUI元素包含在一个可滚动的框架中。
以下是一个示例程序,它将两个`Multiline`元素包含在一个`TKScrollableFrame`中,并实现了它们之间的滚动同步:
```python
import PySimpleGUI as sg
# Define the layout
layout = [
[
sg.TKScrollableFrame(
layout=[
[sg.Multiline(size=(40, 10), key="-MULTI1-")],
[sg.Multiline(size=(40, 10), key="-MULTI2-")]
],
size=(600, 400),
scrollable=True,
vertical_scroll_only=True
)
],
[sg.Button("Exit")]
]
# Create the window
window = sg.Window("Scrollable Frame", layout)
# Create a reference to the scrollable frame
scrollable_frame = window["-TKScrollableFrame-"]
# Create a reference to each multiline element
multi1 = window["-MULTI1-"]
multi2 = window["-MULTI2-"]
# Loop to process window events
while True:
event, values = window.read()
# Exit if the Exit button is clicked or the window is closed
if event == sg.WINDOW_CLOSED or event == "Exit":
break
# Synchronize the scroll positions of the multiline elements
scroll_position = scrollable_frame.vertical_scroll_position
multi1.Widget.yview_moveto(scroll_position)
multi2.Widget.yview_moveto(scroll_position)
# Close the window
window.close()
```
在上面的示例程序中,`TKScrollableFrame`元素包含了两个`Multiline`元素。当用户在其中一个`Multiline`元素中滚动时,代码会将另一个`Multiline`的滚动位置设置为相同的值,从而实现它们之间的滚动同步。
阅读全文