修复自定义 Tkinter 文本框中的输出

问题描述 投票:0回答:1

我有一些创建 GUI 界面的工作代码。 有一个按钮可以运行作业并将一些输出发送到 GUI 中的文本框。 作业运行良好,但输出不同步。 我用于该作业的代码是:

        def UK_VPN_Stats():
            u = username.get()
            p = password.get()
            with open('UK_ASA.txt') as uk_asa:
                for HOST in uk_asa:
                    ASA = {
                        'device_type': 'cisco_ios',
                        'host': HOST,
                        'username': u,
                        'password': p
                    }
                    # Next establish the SSH connection
                    net_connect = ConnectHandler(**ASA)

                    output = net_connect.send_command('sh vpn-sessiondb summary')
                    with open("asa_output-template.txt") as template:
                        fsm = textfsm.TextFSM(template)
                        result = fsm.ParseText(output)
                    for device_stats in result:
                        print_out.insert("end-1c", "Connecting to " + HOST + '\n')
                        print_out.insert("0.0", f"AnyConnect Client: {device_stats[0]}" + '\n')
                        print_out.insert("0.0", f"IKEv2 IPsec: {device_stats[1]}" + '\n')
                        print_out.insert("0.0", f"Total Active and Inactive: {device_stats[2]}" + '\n')
                        print_out.insert("0.0", f"Device Load: {device_stats[3]}" + '\n')

这样输出应该如下所示:

Connecting to device1

-------------------------------------------------------------------------------
AnyConnect Client: 181
IKEv2 IPsec: 1
Total Active and Inactive: 193
Device Load: 26%
-------------------------------------------------------------------------------
Connecting to device2
-------------------------------------------------------------------------------
AnyConnect Client: 164
IKEv2 IPsec: 0
Total Active and Inactive: 171
Device Load: 23%

但是出来的是这样的

Device Load: 23%
Total Active and Inactive: 170
IKEv2 IPsec: 0
AnyConnect Client: 163
Device Load: 26%
Total Active and Inactive: 193
IKEv2 IPsec: 1
AnyConnect Client: 180
Connecting to device1

Connecting to device2

任何帮助我们解决这个问题的帮助将不胜感激。

python textbox customtkinter
1个回答
0
投票

您将文本插入到文本小部件的开头而不是末尾。您要在索引

"0.0"
处插入,tkinter 会将其解释为
"1.0"
,因为行号从 1 开始。如果您希望在末尾插入文本,请使用
"end"

print_out.insert("end", f"AnyConnect Client: {device_stats[0]}" + '\n')
print_out.insert("end", f"IKEv2 IPsec: {device_stats[1]}" + '\n')
print_out.insert("end", f"Total Active and Inactive: {device_stats[2]}" + '\n')
print_out.insert("end", f"Device Load: {device_stats[3]}" + '\n')
© www.soinside.com 2019 - 2024. All rights reserved.