我创建了一个脚本,在窗口中动态地添加/删除/更新标签。
我唯一的问题是旧的框架标签没有消失。。
这个问题会导致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个标签:
新标签在旧标签上: