如何在PySimpleGUI中管理不可见项目的滚动条。

huangapple go评论62阅读模式
英文:

how to manage scrollbar with not visible items in PySimpleGUI

问题

我已经开发了一个PySimpleGUI应用程序,我必须显示一个项目列表,具体取决于从文件中读取的行数。

我知道在PySimpleGUI中动态创建组件是不可能的,所以我定义了一个组件的最大数量,并将它们设置为在文件被读取之前不可见。

在读取之后,我将所需的行数设置为可见,但是Column容器的滚动条仍然无法使用。

初始情况:
如何在PySimpleGUI中管理不可见项目的滚动条。

读取后:
如何在PySimpleGUI中管理不可见项目的滚动条。

我还尝试让行的一个项目始终可见,在这种情况下,滚动条可以正常使用:

始终可见的初始情况:
如何在PySimpleGUI中管理不可见项目的滚动条。

始终可见的读取后:
如何在PySimpleGUI中管理不可见项目的滚动条。

代码:

	def getLayoutFramePars():
		sub_layout = []

		for node_index in range(30):
			line = addParLine(node_index)
			sub_layout.append(line)

		layout = [
			[
				sg.Col(
					layout=sub_layout,
					background_color=Blue4,
					expand_x=True,
					expand_y=True,
				)
			]
		]

		return layout


	def addParLine(index):
		text_index = str(index)

		if index < 10:
			text_index = "0" + text_index

		KEY_LABEL_INDEX = PREFIX_PAR_LABEL_INDEX + text_index
		KEY_LABEL_NAME = PREFIX_PAR_LABEL_NAME + text_index
		KEY_LABEL_VALUE = PREFIX_PAR_LABEL_VALUE + text_index
		KEY_BUTTON_READ = PREFIX_PAR_BUTTON_READ_VALUE + text_index
		KEY_BUTTON_SEND = PREFIX_PAR_BUTTON_SEND_VALUE + text_index

		layout = [
			sg.Text(
				key=KEY_LABEL_INDEX,
				text=text_index,
				size=LABEL_SIZE_05,
				justification=ALIGN_CENTER,
				visible=False,
				pad=5,
			),

			sg.Text(
				'',
				key=KEY_LABEL_NAME,
				size=LABEL_SIZE_20,
				relief=sg.RELIEF_SUNKEN,
				justification=ALIGN_CENTER,
				visible=False,
				pad=5,
			),

			sg.InputText(
				'',
				key=KEY_LABEL_VALUE,
				size=LABEL_SIZE_30,
				justification=ALIGN_LEFT,
				text_color=Blue2,
				background_color=LightGrey,
				enable_events=True,
				visible=False,
				pad=5,
			),

			sg.Button(
				key=KEY_BUTTON_READ,
				button_text='Leggi',
				size=BUTTON_SIZE_14,
				button_color=BUTTON_COLOR_BLUE,
				visible=False,
				pad=5,
			),

			sg.Button(
				key=KEY_BUTTON_SEND,
				button_text='Invia',
				size=BUTTON_SIZE_14,
				button_color=BUTTON_COLOR_BLUE,
				visible=False,
				pad=5,
			),
		]

		return layout


	def initLayoutBody():
		layout = [
			[
				sg.Text(
					text="##",
					size=LABEL_SIZE_05,
					text_color=DarkOrange,
					justification=ALIGN_CENTER,
					pad=5,
				),

				sg.Text(
					text="Nome",
					size=LABEL_SIZE_20,
					text_color=DarkOrange,
					justification=ALIGN_CENTER,
					pad=5,
				),

				sg.Text(
					text="Valore",
					size=LABEL_SIZE_30,
					text_color=DarkOrange,
					justification=ALIGN_CENTER,
					pad=5,
				),
			],
			[
				sg.Column(
					key=KEY_FRAME_PARS,
					size=(750, 300),
					layout=getLayoutFramePars(),
					element_justification=ALIGN_LEFT,
					scrollable=True,
					vertical_scroll_only=True,
					background_color=DarkGreen,
				),
			]
		]

		return layout
英文:

i have developed a PySimpleGUI application and i must show a list of items depending from the number of rows readed from a file.
I know that is not possible to create components dinamically in PySimpleGUI, so i've defined a max number of components and set they as not visible until the file is readed.

After the reading, i set the desired number of rows as visible, but the scrollbar of the Column container remains unusable.

Initial situation:
如何在PySimpleGUI中管理不可见项目的滚动条。

After the reading:
如何在PySimpleGUI中管理不可见项目的滚动条。

I also try to leave an item of the row always visibile, and in this case scrollbar works well:

Initial situation with index always visible:
如何在PySimpleGUI中管理不可见项目的滚动条。

After the reading with index always visible:
如何在PySimpleGUI中管理不可见项目的滚动条。

Code:

def getLayoutFramePars():
sub_layout = []
for node_index in range(30):
line = addParLine(node_index)
sub_layout.append(line)
layout = [
[
sg.Col(
layout=sub_layout,
background_color=Blue4,
expand_x=True,
expand_y=True,
)
]
]
return layout
def addParLine(index):
text_index = str(index)
if index &lt; 10:
text_index = &quot;0&quot; + text_index
KEY_LABEL_INDEX = PREFIX_PAR_LABEL_INDEX + text_index
KEY_LABEL_NAME = PREFIX_PAR_LABEL_NAME + text_index
KEY_LABEL_VALUE = PREFIX_PAR_LABEL_VALUE + text_index
KEY_BUTTON_READ = PREFIX_PAR_BUTTON_READ_VALUE + text_index
KEY_BUTTON_SEND = PREFIX_PAR_BUTTON_SEND_VALUE + text_index
layout = [
sg.Text(
key=KEY_LABEL_INDEX,
text=text_index,
size=LABEL_SIZE_05,
justification=ALIGN_CENTER,
visible=False,
pad=5,
),
sg.Text(
&#39;&#39;,
key=KEY_LABEL_NAME,
size=LABEL_SIZE_20,
relief=sg.RELIEF_SUNKEN,
justification=ALIGN_CENTER,
visible=False,
pad=5,
),
sg.InputText(
&#39;&#39;,
key=KEY_LABEL_VALUE,
size=LABEL_SIZE_30,
justification=ALIGN_LEFT,
text_color=Blue2,
background_color=LightGrey,
enable_events=True,
visible=False,
pad=5,
),
sg.Button(
key=KEY_BUTTON_READ,
button_text=&#39;Leggi&#39;,
size=BUTTON_SIZE_14,
button_color=BUTTON_COLOR_BLUE,
visible=False,
pad=5,
),
sg.Button(
key=KEY_BUTTON_SEND,
button_text=&#39;Invia&#39;,
size=BUTTON_SIZE_14,
button_color=BUTTON_COLOR_BLUE,
visible=False,
pad=5,
),
]
return layout
def initLayoutBody():
layout = [
[
sg.Text(
text=&quot;##&quot;,
size=LABEL_SIZE_05,
text_color=DarkOrange,
justification=ALIGN_CENTER,
pad=5,
),
sg.Text(
text=&quot;Nome&quot;,
size=LABEL_SIZE_20,
text_color=DarkOrange,
justification=ALIGN_CENTER,
pad=5,
),
sg.Text(
text=&quot;Valore&quot;,
size=LABEL_SIZE_30,
text_color=DarkOrange,
justification=ALIGN_CENTER,
pad=5,
),
],
[
sg.Column(
key=KEY_FRAME_PARS,
size=(750, 300),
layout=getLayoutFramePars(),
element_justification=ALIGN_LEFT,
scrollable=True,
vertical_scroll_only=True,
background_color=DarkGreen,
),
]
]
return layout

Could someone help me to understand what i do wrong and how to solve this problem?

Thanks

答案1

得分: 1

以下是已翻译的内容:

  • window.refresh() 以更新 GUI。
  • window[column_key].contents_changed() 以使新滚动区域匹配新内容。
英文:

Call following methods after you update the content of the column.

  • window.refresh() to update the GUI.
  • window[column_key].contents_changed() to update the new scroll area
    to match the new contents.

huangapple
  • 本文由 发表于 2023年2月8日 17:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定