[英]Dynamically change row size PySimpleGUI
I am trying to dynamically display rows in the UI when a button is pressed.我试图在按下按钮时在 UI 中动态显示行。
To do this, I am first declaring the row in the layout, but making it hidden.为此,我首先在布局中声明该行,但将其隐藏。 Then when I get the value, I make the row hidden.然后当我得到值时,我隐藏了行。
Sometimes the text is very large, so I am using the textwrap's fill method in python.有时文字很大,所以我在python中使用textwrap的填充方法。 So this gives me text with 2 to 3 rows.所以这给了我 2 到 3 行的文本。
But when I try to, each character appears in different rows.但是当我尝试时,每个字符出现在不同的行中。
disp_text = "Text:\n{}".format(textwrap.fill(some_text,70))
size = (70,disp_text.count("\n")+1)
window['-TEXT-'].set_size(size)
window['-TEXT-'].update(value = disp_text,visible = True)
Assume some_text
is "Hello how are you....(and some 50 other characters)"
假设some_text
是"Hello how are you....(and some 50 other characters)"
Now, with the above string, the size
is (70,3)
现在,使用上面的字符串, size
为(70,3)
So, ideally it should wrap some_text
into 3 lines.因此,理想情况下,它应该将some_text
包装成 3 行。
But the actual output in the GUI is:但是GUI中实际的output是:
H
e
l
Which is basically the first 3 characters in some_text
in 3 seperate lines instead of some_text
as a while in 3 different lines.这基本上是some_text
在 3 个单独的行中的前 3 个字符,而不是some_text
在 3 个不同的行中的一段时间。
How does one fix this?如何解决这个问题?
Print your disp_text to make sure it's got what you think it does.打印您的 disp_text 以确保它符合您的想法。
Here's one approach.这是一种方法。
import PySimpleGUI as sg
def main():
layout = [ [sg.Text('My Window')],
[sg.Text(size=(20,1), auto_size_text=False, key='-OUT-')],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Go':
window['-OUT-'].set_size((20, 10))
window['-OUT-'].update('This is my output string\nThat spans multiple rows\nAll the way to 3\n'+
'It will even wrap if the string is too long')
wraplen = window['-OUT-'].Widget.winfo_reqwidth() # width tkinter says widget will be
window['-OUT-'].Widget.configure(wraplen=wraplen) # set wrap to width of widget to match contents
window.close()
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.