界面更新慢,如何能提高界面刷新速度?

新建了如下的一个界面:
界面更新慢,如何能提高界面刷新速度?
需要实现:每勾选上一个checkbutton,会在新的界面上显示文件中的过滤内容,如上界面会显示:
界面更新慢,如何能提高界面刷新速度?
定义如下函数作为每一个checkbutton的共同的command:command=AllFunc.AnaCheckedFunc
def AnaCheckedFunc(self):

…..
with open(self.AnaFileName, “r”, encoding=”ISO-8859-1”) as f:

 lines = f.readlines()
 for line in lines:
    if self.FiltKeyWord2.get() == 1:
      if "HBTrackM1".lower() in line.lower():
       self.FilterContents = self.FilterContents + line
       self.FilterKeyWordsColor.update({'HBTrackM1': '#DC143C'})
    ....
self.AnalysisWinMainShow(self.FilterKeyWordsColor, self.FilterContents)

def AnalysisWinMainShow(self,keywordsColor,content):

if self.AnalysisWinMain != None:
    self.AnalysisWinMain.destroy()

self.AnalysisWinMain =tk.Tk()
self.AnalysisWinMain.title("Show Logs")
self.AnalysisWinMain.geometry("1000x600")
# ShowLogWin.resizable(0,0)
winMainFrame = tk.Frame(self.AnalysisWinMain)
winMainFrame.pack(side=tk.LEFT, fill=tk.Y)
winYScroll = tk.Scrollbar(winMainFrame)
winYScroll.pack(side=tk.RIGHT, fill=tk.Y)
winXScroll = tk.Scrollbar(winMainFrame, orient=tk.HORIZONTAL)
winXScroll.pack(side=tk.BOTTOM, fill=tk.X)
winText = tk.Text(winMainFrame, wrap=tk.NONE, width=800, height=600, xscrollcommand=winXScroll.set,yscrollcommand=winYScroll.set)
winXScroll.config(command=winText.xview)
winYScroll.config(command=winText.yview)

winText.pack(expand=tk.YES, side=tk.TOP, fill=tk.Y)
winText.insert('insert', content)
self.KeyWordHightLight(keywordsColor, winText)
self.AnalysisWinMain.protocol('WM_DELETE_WINDOW', self.AnalysisWinMainUpdate)
self.AnalysisWinMain.mainloop()

以上程序运行下来,基本功能是可以实现的,但每次运行下来,总觉得界面更新太慢了。
有没有什么办法,能让程序运行得更快一些呢?
感谢!!!

讨论数量: 5
Jason990420

可以使用以下方式改善

  • self.AnalysisWinMain 在代码开始运行时, 只建立一次
  • self.AnaFileName 只读入一次
  • 方法 self.AnaCheckedFunc 只删除 winText 全部旧的内容, 再插入新的内容.

Example Code

import tkinter as tk

class GUI:

    def __init__(self, text):
        lines = text.strip().split('\n')
        self.fruits = {}
        width = 0
        for line in lines:
            if ' - ' in line:
                fruit, _, memo = line.partition(' - ')
                self.fruits[fruit] = memo
                width = max(width, len(fruit))
        width += 5

        self.root = tk.Tk()
        self.root.title("Show Logs")

        self.frame1 = tk.Frame(self.root)
        self.frame1.pack(side=tk.TOP, fill=tk.X)
        self.checks = [tk.Checkbutton(self.frame1, text=fruit, width=width,
            command=lambda fruit=fruit:self.callback(fruit))
                for fruit in self.fruits.keys()]
        for check in self.checks:
            check.pack(side=tk.LEFT)

        self.frame2 = tk.Frame(self.root)
        self.frame2.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
        self.scroll = tk.Scrollbar(self.root)
        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.text = tk.Text(self.root, width=20, height=5, yscrollcommand=self.scroll.set)
        self.text.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
        self.scroll.configure(command=self.text.yview)

        self.root.mainloop()

    def callback(self, fruit):
        memo = self.fruits[fruit]
        self.text.delete("1.0", tk.END)
        self.text.insert(tk.END, fruit+'\n'+memo)

text = """
Apple - the usually round, red or yellow, edible fruit of a small tree, Malus sylvestris, of the rose family.
Banna - any of several tropical and subtropical herbaceous treelike plants of the musaceous genus Musa, esp M. sapientum, a widely cultivated species propagated from suckers and having hanging clusters of edible fruit
Guava - any of numerous tropical and subtropical American trees or shrubs belonging to the genus Psidium, of the myrtle family, especially P. guajava, bearing large, yellow, round to pear-shaped fruit, and P. littorale, bearing smaller, yellowish to deep-red, oval fruit.
Lemon - the yellowish, acid fruit of a subtropical citrus tree, Citrus limon.
Mango - the oblong, sweet fruit of a tropical tree, Mangifera indica, of the cashew family, eaten ripe, or preserved or pickled.
"""
GUI(text)

file

1年前 评论
AnSonAnSon (楼主) 1年前
AnSonAnSon (楼主) 1年前

@Jason990420 感谢您的答复,可能我的功能需求没说清楚,我的需求如下: file 1处导入一个文件,2处列出一系列关键字,根据关键字的是否勾选,从文件中过滤出带关键字的每一行内容,以特殊的颜色将关键字标注出来。 我目前的基本功能已经实现了,但按照我这个程序设计,每次执行都需要耗费很长的时间,效率很低,想请教一下效率比较高的实现方法,感谢!

1年前 评论
Jason990420

如果只是使用纯 python 语句来作大量的运算或搜索, 肯定速度会很慢, 试试以下方式

  • 调用 C++ 函数, 或
  • 调用 re 库, 或
  • 文件分割多线程处理, 或
  • 文件预先处理, 或
  • 其他方式
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!