【问题标题】:How can I print more than one line of words/phrases in a Multiline PySimpleGUI element?如何在多行 PySimpleGUI 元素中打印多行单词/短语?
【发布时间】:2021-10-21 08:55:03
【问题描述】:

下面的小程序创建一个带有 Multiline 元素、一个 Input 元素和一个按钮的窗口。当我在输入字段中键入一个单词或短语时,该单词/短语也会打印在 Multiline 元素中。但是当我输入第二个单词/短语时,第一个单词/短语会被多行中的第二个替换。如何在 Multiline 元素中保留两个或多个,一个在另一个下方(多行)? 代码如下:

import PySimpleGUI as sg

sz = (7,1)

column1 = [ [sg.ReadButton('UPDATE\nTEXT', size=sz)],
            [sg.ReadButton('LOAD-\nTEXT', size=sz)],
            [sg.ReadButton('SAVE-VOC', size=sz)],
            [sg.ReadButton('CLEAR\nTEXT', size=sz)]
            ]

column2 = [[sg.ReadButton('Click here')]]

col_layout = [
                [sg.InputText(focus=True, key='word')],
                [sg.Column(column2)]
             ]

layout = [
            [sg.Text("Study Text Box")],
            [sg.Multiline(size=(50,10), font='Tahoma 13', key='-STLINE-', autoscroll=True), sg.Column(column1), sg.VerticalSeparator(pad=None), sg.Column(col_layout)]
         ]
        

window = sg.Window("TbLLT Program", layout, resizable=True, finalize=True)

while True:
    event, values=window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'SAVE-VOC':
        with open('someText(saved).txt', 'w+') as file:
            savedText1 = file.write(values['-STLINE-'])
        file.close()
    if event == 'Click here':
        window['-STLINE-'].update(values['word'])
window.close()

【问题讨论】:

    标签: input multiline pysimplegui


    【解决方案1】:

    使用方法update将替换sg.Multiline的全部内容,或者多一个选项append=True,但需要时需要自己添加'\n'

    使用sg.Multiline 的方法print 像Python 正常打印一样打印,除了将输出路由到多行元素并在需要时添加颜色。

    def print(self, *args, end=None, sep=None, text_color=None,
        background_color=None, justification=None, font=None, colors=None, t=None,
        b=None, c=None,  autoscroll=True):
    

    这样称呼

            window['-STLINE-'].print(values['word'])
    

    或使用选项append=True 调用方法update

            window['-STLINE-'].update(values['word']+'\n', append=True)
    

    【讨论】:

    • 谢谢杰森,使用你建议的最后两行中的任何一个都可以解决我的问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多