【问题标题】:Append text to textbox in pysimplegui?在pysimplegui中将文本附加到文本框?
【发布时间】:2020-12-03 12:59:08
【问题描述】:

所以我正在尝试启动并运行一个简单的程序来检查 modbus 网络的 GUI。这需要一个巨大的文本字段来显示数据。我正在使用 sg.Text。我遇到的问题是更新时没有附加文本框。我确定我遗漏了一些明显的东西。我需要附加因为程序将如何检查网络然后可能保存整个文本框。我如何使也是可滚动的?我觉得我脑子里放了一个大屁。


import PySimpleGUI as sg
import os.path

# First the window layout in 2 columns

file_list_column = [
    [
        sg.Text("Search Network?"),
        sg.Button('Read Serial'),
        sg.Button('Test Modbus'),
    ],
    [
        sg.Text("Textbox test.", size=(30, 5),  relief=sg.RELIEF_SUNKEN, background_color='white', text_color='black',
            enable_events=True, key="-NETWORK LIST-")

    ],
    [
        sg.Input(key='-IN-')
    ],
    [
        sg.Button('Test'),
        sg.Button('Clear'),
        sg.Button('Exit')
    ],
]


# ----- Full layout -----
layout = [
    [
        sg.Column(file_list_column),
        sg.VSeperator(),
    ]
]

window = sg.Window("Network Troubleshooter", layout)

# Run the Event Loop
while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break


    if event == 'Test':
        # Update the "output" text element to be the value of "input" element
        window['-NETWORK LIST-'].update(values['-IN-'])


    if event == 'Clear':
        # Update the "output" text element to be the value of "input" element
        window['-NETWORK LIST-'].update('')


window.close()

【问题讨论】:

    标签: python pysimplegui


    【解决方案1】:

    sg.Text 是一个标签,没有附加功能。您可以通过将字符串添加到显示文本的当前值,然后更新它来做到这一点。

    text = window['-NETWORK LIST-']
    text.update(text.get()+'New Text append')
    

    元素sg.Text 没有滚动条,sg.Multiline 是首选,并且附加文本的方式相同。

    【讨论】:

    • 顺便说一句,使用 tkinter 版本的 PySimpleGUI,将 Text 元素高度设置为 None 是可能的,结果是元素总是调整大小以适应内容。例如 (20,None) 的大小。
    • 那里真的很棒。是的,禁用的多线是要走的路。我用错了。
    猜你喜欢
    • 2020-03-12
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2014-11-05
    相关资源
    最近更新 更多