【问题标题】:Dynamically change row size PySimpleGUI动态更改行大小 PySimpleGUI
【发布时间】:2021-03-11 01:18:17
【问题描述】:

我试图在按下按钮时在 UI 中动态显示行。

为此,我首先在布局中声明该行,但将其隐藏。然后当我得到值时,我隐藏了行。

有时文本很大,所以我在python中使用textwrap的填充方法。所以这给了我 2 到 3 行的文本。

但是当我尝试时,每个字符出现在不同的行中。

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)

假设some_text"Hello how are you....(and some 50 other characters)"

现在,有了上面的字符串,size 就是(70,3)

因此,理想情况下,它应该将some_text 包装成 3 行。

但在 GUI 中的实际输出是:

H
e
l

这基本上是 some_text 中的前 3 个字符,分 3 行,而不是 some_text 的前 3 个字符。

如何解决这个问题?

【问题讨论】:

    标签: python user-interface pysimplegui


    【解决方案1】:

    打印您的 disp_text 以确保它符合您的想法。

    这是一种方法。

    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()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多