python tkinter界面增加调试信息_如何在Tkinter窗口中动态添加/删除/更新标签?

本文介绍了一个使用Python的Tkinter库创建的脚本,该脚本在窗口中动态显示ADB连接设备的网络配置。当设备数量变化时,脚本存在问题:旧的标签没有被正确删除,导致窗口背景中残留标签并可能引起内存问题。作者展示了如何添加、更新设备标签,但删除部分未能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我创建了一个脚本,在窗口中动态地添加/删除/更新标签。

我唯一的问题是旧的框架标签没有消失。。

这个问题会导致windows的bg中出现一些标签干扰,当然这也会导致某种内存泄漏(不确定这是否是恰当的术语)。在

这是我的代码:import tkinter as tk

from tkinter.ttk import *

from subprocess import call,Popen,PIPE, STDOUT

class App():

def __init__(self):

self.root = tk.Tk()

self.root.title("devices networks")

self.update_clock()

self.root.mainloop()

def update_clock(self):

i=0

adb_absolute_path = "C:\\Users\\ilan.MAXTECH\\AppData\\Local\\Android\\Sdk\\Platform-tools\\"

# Get the list of connected devices

cmd = adb_absolute_path+"adb.exe devices"

proc = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)

device_list = proc.communicate()[0].decode().split("\r\n")

# remove unnecessary text in devices call

device_list.pop(0)

device_list.remove("")

device_list.remove("")

#### not working.... #######

# #erase the old labels ( in case a device has been disconected

# for line in range(10):

# lb = Label(self.root, text="")

# lb.grid(row=1, column=line)

###########################

#print netcfg for each device

for device in device_list:

#get the netcfg for specific device

device_serial = device.split("\t")[0]

cmd = adb_absolute_path + "adb.exe -s " + device_serial + " shell netcfg"

proc = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)

netcfg_output = proc.communicate()[0].decode()

#add a new label to the screen

lb = Label(self.root, text=device_serial+"\n"+netcfg_output)

lb.grid(row=1, column=i)

lbblank = Label(self.root,text="\t\t")

lbblank.grid(row=1, column=i+1)

i += 2

self.root.geometry(str(device_list.__len__()*450)+"x700")

self.root.after(1000, self.update_clock)

app=App()

以下是一些屏幕截图:

连接3个设备,然后显示3个标签:

连接2个设备,然后显示2个标签:

新标签在旧标签上:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值