我正在使用python tkinter GUI。我的问题是当我在ScrolledText小部件中插入大量数据(大于200 KB)时,thinter gui挂起。有没有解决办法用tkinter GUI处理大量数据?
这是我的代码:
import Tkinter
from Tkinter import *
import tkFileDialog
from tkMessageBox import showinfo, showerror
from ScrolledText import ScrolledText
import codecs
import random
class GUI_Window():
'''
GUI Class
'''
def init(self):
self.__m_top = Tkinter.Tk()
self.lab1 = Tkinter.Label(self.__m_top, font = "{Courier} 10 {bold}" ,text = "Data")
self.lab1.pack()
self.lab1.place(bordermode=OUTSIDE, height=25, width=40,x=10,y=20)
self.__i_txtnm="Data Window"
self.__Data_window_txt = ScrolledText(self.__m_top, undo=False,height=5,width=4)
self.__Data_window_txt['font'] = ('Times New Roman', '10')
self.__Data_window_txt.bind("<Control-Key-a>", lambda event, arg=self.__i_txtnm: self.__select_alldata(event, arg))
self.__Data_window_txt.pack(expand=False, fill='both')
self.__Data_window_txt.place(bordermode=OUTSIDE,height=450,width=400,x=10,y=40)
self.lab1 = Tkinter.Label(self.__m_top, font = "{Courier} 10 {bold}" ,text = "Data Size")
self.lab1.pack()
self.lab1.place(bordermode=OUTSIDE, height=25, width=100,x=100,y=500)
self.tfield = Tkinter.Text(self.__m_top,font = "{Courier} 10 {normal}",width = 82,height = 20,background = 'white', foreground = 'black')
self.tfield.pack()
self.tfield.place(bordermode=OUTSIDE, height=25, width=90,x=190,y=500)
self.lab2 = Tkinter.Label(self.__m_top, font = "{Courier} 10 {bold}" ,text = "KB")
self.lab2.pack()
self.lab2.place(bordermode=OUTSIDE, height=25, width=20,x=290,y=500)
self.__btnText="Generate Data"
self.__gen_Data_btn=Tkinter.Button(self.__m_top, text = "Generate Data", command =lambda: self.__generate_btn_click() , width = 10)
self.__gen_Data_btn.pack()
self.__gen_Data_btn.place(bordermode=OUTSIDE, height=25, width=90,x=140,y=550)
self.__btnText="Clear Data"
self.__gen_Data_btn=Tkinter.Button(self.__m_top, text = "Clear Data", command =lambda: self.__claerData() , width = 10)
self.__gen_Data_btn.pack()
self.__gen_Data_btn.place(bordermode=OUTSIDE, height=25, width=90,x=240,y=550)
self.__m_top.wm_title('My Data Window')
self.__m_top.geometry("450x650")
self.__m_top.resizable('0', '0')
self.__m_top.mainloop()
#self.__browse_sendFilepath_txt.insert(INSERT,file.name)
def __generate_btn_click(self):
data_Size=self.tfield.get("1.0", END)
try:
data_Size= int(data_Size)
data=self.__genrateData(data_Size)
self. __Data_window_txt.insert(INSERT, data)
except:
showerror('Set Config', 'Plesase Insert Numeric Value')
def __select_alldata(self,event,i_txtnm):
self.__Data_window_txt.tag_add(SEL, "1.0", END)
self.__Data_window_txt.mark_set(INSERT, "1.0")
self.__Data_window_txt.see(INSERT)
def __genrateData(self,i_size):
data=''
for i in range(1024*i_size):
data+=random.choice("abcdefghijklmnopqrstuvwxyz")
return data
def __claerData(self):
self.__Data_window_txt.delete(1.0, END)
if __name__ == "__main__":
g1=GUI_Window()
g1.init()