0

我正在尝试在 Tkinter 中做一个 GUI,并且我在 textpad 中有 100 万个条目。因此,我将一个函数绑定到应在鼠标单击时调用的每个条目。但是随着条目被插入到文本板并绑定到函数,在 60 万个条目之后,GUI 开始冻结(我目前正在使用 python-SQL 来减少 RAM 上的内存使用)。

traces=sql_database.debug_read(id_count)    #reading data from SQL
x1=0     #tag number for binding
for i in range(len(traces)): 
    Id,t_s,tra_id,t_d=traces[i]    #splitting data to be printed
    m,g,s_t=dict2[tra_id]          #checking data with a dictionary 
    filtered_data=t_s+tra_id+t_d
    data_to_print=str(t_s)+'\t '+m+'\t '+g+'\t '+s_t
    textPad.insert('end',data_to_print,x1)
    if i%20000==0: 
          mainWindow.update() 
          textPad.see(END)
    textPad.tag_bind(x1,'<1>'(lambda e,x1=x1:decoder_fun(x1,t_d)))
    x1=x1+1

没有事件绑定,GUI 工作正常。cpu 使用率和 RAM 使用率中等,有绑定。

4

1 回答 1

0

我没有使用 textpad ,而是使用了列表框,它提供了获取列表框中每个实体的好方法。在列表框事件中,我们可以获得发生鼠标单击的索引,我可以使用该索引与列表框中的各个条目进行交互(调用函数)。我只需要一个绑定整个列表框条目

import Tkinter
from Tkinter import *

def callback(event):
    index=w.curselection()
    #print index
    print w.get(index)


root=Tkinter.Tk()
scrollbar = Tkinter.Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
w=Tkinter.Listbox(root)
w.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=w.yview)
for i in range(20):
    w.insert(i,i)
w.pack()
def ons():
    w.delete(1,END)
w.bind('<<ListboxSelect>>',callback)
b=Tkinter.Button(root,command=ons)
b.pack()
root.mainloop()

curselection() 将给出发生鼠标点击的索引。get(index) 将给出该索引号的列表框中的条目。我们必须使用“ListboxSelect”绑定来为鼠标点击事件使用这些方法。更多选项在此链接中:https ://www.tutorialspoint.com/python/tk_listbox.htm

于 2017-04-04T11:13:55.150 回答