英文:
how to manage scrollbar with not visible items in PySimpleGUI
问题
我已经开发了一个PySimpleGUI应用程序,我必须显示一个项目列表,具体取决于从文件中读取的行数。
我知道在PySimpleGUI中动态创建组件是不可能的,所以我定义了一个组件的最大数量,并将它们设置为在文件被读取之前不可见。
在读取之后,我将所需的行数设置为可见,但是Column容器的滚动条仍然无法使用。
我还尝试让行的一个项目始终可见,在这种情况下,滚动条可以正常使用:
代码:
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.
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:
After the reading with index always visible:
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 < 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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论