首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于大量entry...crashes图形用户界面的python Tkinter textpad事件绑定

用于大量entry...crashes图形用户界面的python Tkinter textpad事件绑定
EN

Stack Overflow用户
提问于 2017-03-31 18:32:09
回答 1查看 141关注 0票数 0

我正在尝试用Tkinter做一个图形用户界面,我在textpad中有一百万个条目。因此,我将一个函数绑定到每个应该在鼠标单击时调用的条目。但是,当条目被插入到textpad并绑定到函数时,在60万个条目之后,GUI开始冻结(我目前使用python-SQL来减少RAM上的内存使用量)。

代码语言:javascript
复制
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使用率为中等,具有绑定..

EN

回答 1

Stack Overflow用户

发布于 2017-04-04 19:13:55

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

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43138520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档