几圈之后tkinter冻结了

时间:2018-01-27 15:26:27

标签: python memory tkinter tk

我编写了一个简单的应用程序,可以从字典列表中动态创建标签。 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()

1 个答案:

答案 0 :(得分:-1)

现在,您有deletingdestroying个窗口小部件。结果,它们只是建立起来,导致脚本变慢并使用增加的内存。您可以在for slave in nazwa_typ.grid_slaves(): slave.destroy()之后添加nr_+=1来解决此问题。