PythonTkinter textpad事件绑定大量条目…崩溃GUI

2024-04-20 10:50:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个图形用户界面在Tkinter和我有100万个条目到textpad。因此,我将一个函数绑定到每个应该在鼠标单击时调用的条目。但是当条目被插入textpad并绑定到函数时,在60万条条目之后,GUI开始冻结(我现在正在使用pythonsql来减少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使用率中等,有绑定。。你知道吗


Tags: to函数idfordatatag条目gui
1条回答
网友
1楼 · 发布于 2024-04-20 10:50:33

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

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

相关问题 更多 >