After()与Python中的Destroy()冲突

时间:2016-07-08 07:38:13

标签: python user-interface tkinter

使用 after() destroy()方法后出现问题。当我监控ram状态和处理器状态时,我创建了一个meniu。当我第一次拨打电话时,它没问题,但是当我拨打第二个def,例如处理器状态时,帧内的信息显示几毫秒,然后显示有关ram状态的信息,并再次通过并结束。

http://i.imgur.com/SqAeBUN.gif

def ram():
root.after(1000,ram)
for widget in app.winfo_children():
    widget.destroy()
iram = psutil.virtual_memory()
total = iram.total / (1024*1024)
available = iram.available /(1024*1024)
percent = iram.percent
used = iram.used /(1024*1024)

inf = Label(app, text="Information about RAM status")
inf.grid(row=0,column=1, pady=10)

total_1 = Label(app, text="Total memory RAM")
total_1.grid(row=1,column=0)

total1 = Label(app, text=total,fg="blue")
total1.grid(row=1, column=1,pady=5)

available_1 = Label(app, text="Available memory RAM")
available_1.grid(row=2,column=0)

available1 = Label(app, text=available,fg="blue")
available1.grid(row=2, column=1,pady=5)

percent_1 = Label(app, text="Used memory RAM in %")
percent_1.grid(row=3,column=0)

percent1 = Label(app, text=percent,fg="red")
percent1.grid(row=3, column=1,pady=5)

used_1 = Label(app, text="Used memory RAM")
used_1.grid(row=4,column=0)

used1 = Label(app, text=used,fg="blue")
used1.grid(row=4, column=1,pady=5)

def help():
for widget in app.winfo_children():
    widget.destroy()
help_1=Label(app, text="This aplication was created with Python 3.5 \n")
help_1.grid(row=0,column=0)

root = Tk()
app = Frame(root)
app.grid()
root.title("S.M.A.R.T.")
root.geometry("500x300")
root.resizable(0,0)
menubar = Menu(root)

filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Battery Status", command=battery_status)
filemenu.add_command(label="Disk's Status", command=disk_status)
filemenu.add_command(label="Processor Status", command=processor)
filemenu.add_command(label="PC's Status", command=pc)
filemenu.add_command(label="RAM's Status", command=ram)
filemenu.add_command(label="Windows Status", command=windows)
filemenu.add_command(label="Exit", command=root.quit)

filemenu.add_separator()

menubar.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help", command=help)
menubar.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_separator()
root.config(menu=menubar)
root.mainloop()
root.destroy()

我应该做些什么来避免这个问题?因为它回想起那些子菜单,并且无法理解框架中的信息。

1 个答案:

答案 0 :(得分:0)

问题尚不清楚,但我认为您提出的问题是如何在请求不同信息时停止更新旧信息。

after_cancel返回一个标识符,您可以将其传递给global after_id after_id = None ... def cancel_old_job(): global after_id if after_id is not None: root.after_cancel(after_id) after_id = None ... def ram(): global after_id cancel_old_job() after_id = root.after(1000,ram) ... def pc(): global after_id cancel_old_job() after_id = root.after(1000,pc) ... ... ,以防止再次调用该函数。因此,在安排新作业之前,您的菜单命令只需要取消任何待处理作业。

它看起来像这样:

self