我编写了一个简单的应用程序,可以从字典列表中动态创建标签。my dict
字典是从api获取数据的结果,但在下面的示例代码中,我更改了它以最小化代码。你知道吗
这项工作如我所料,但经过几次循环,它是缓慢的,停止响应。你知道吗
我怎么能解决这个问题?你知道吗
提前感谢您的帮助!你知道吗
from tkinter import *
import threading
import time
idn = 0
zbior_naglowkow = ('id' , 'date',)
zbior_frame = ('BC', 'LC', )
mydict = [{'typ': 'LC', 'id': 12, 'date': '2018-01-26 19:49:15'}, {'typ': 'LC', 'id': 16, 'date': '2018-01-16 19:49:15'}, {'typ': 'BC', 'id': 1, 'date': '2018-01-26 19:49:15'}, {'typ': 'BC', 'id': 2, 'date': '2018-01-26 19:49:15'}, {'typ': 'LC', 'id': 3, 'date': '2018-01-26 19:49:15'}, {'typ': 'BC', 'id': 4, 'date': '2018-01-26 19:49:15'}, {'typ': 'BC', 'id': 5, 'date': '2018-01-26 19:49:15'}]
def raise_frame(frame):
frame.tkraise()
def ap():
threading.Timer(0, apka).start()
def dane():
zbior_frame = ('BC', 'LC', )
nr_ = 1
nn = 0
for nazwa_typ in zbior_frame:
row_naglowek = 190
row_oferta = 200
col_id = 30
col_cena_zakupu_obliczona = 40
col_date = 50
ramka = 'f'+str(nr_)+'_'+nazwa_typ
ramka = eval(ramka)
nr_ +=1
for offert in mydict:
if offert['typ'] == nazwa_typ:
naglowek_Label = Label(ramka, text=zbior_naglowkow[0])
naglowek_Label.grid(row = row_naglowek, column = col_id)
id_Label = Label(ramka, text=offert['id'])
id_Label.grid(row = row_oferta, column = col_id)
naglowek_Label = Label(ramka, text=zbior_naglowkow[1])
naglowek_Label.grid(row = row_naglowek, column = col_date)
date = offert['date']
date_Label = Label(ramka, text=date)
date_Label.grid(row = row_oferta, column = col_date)
row_oferta += 10
col_id +=10
col_date += 10
def apka():
global idn
idn += 1
mydict.append({'typ' : 'LC' , 'id': idn , 'date' : '2018-01-26 19:49:15',})
print(mydict)
dane()
root.after(1000,apka)
root = Tk()
root.title('App')
root.geometry('1200x900')
f0_dashboard = Frame(root)
f1_BC = Frame(root)
f2_LC = Frame(root)
zbior_fnr = (f0_dashboard, f1_BC , f2_LC,)
for frame in zbior_fnr:
frame.grid(row=0, column=0, sticky='news')
f0_dashboard_button = Button(frame, bg='blue', width=10, text = 'dashboard', command=lambda:raise_frame(f0_dashboard))
f0_dashboard_button.grid(row=110, column=30)
f1_BC_button = Button(frame, bg='blue', width=10, text = 'BC', command=lambda:raise_frame(f1_BC))
f1_BC_button.grid(row=120, column=30)
f2_LC_button = Button(frame, bg='blue', width=10, text = 'LC', command=lambda:raise_frame(f2_LC))
f2_LC_button.grid(row=120, column=40)
start_app_button = Button(frame, bg='blue', width=10, text = 'start', command=ap)
start_app_button.grid(row=100, column=50)
mainloop()
现在你有了
deleting
或destroying
小部件。因此,它们只是在累积,导致脚本速度变慢,并占用越来越多的内存。您可以通过在nr_+=1
之后添加for slave in nazwa_typ.grid_slaves(): slave.destroy()
来解决这个问题。你知道吗相关问题 更多 >
编程相关推荐