随着时间的流逝,Tkinter应用程序的响应速度越来越慢

时间:2018-09-16 06:10:41

标签: python tkinter

长时间运行时,我的TKinter应用程序出现问题。该应用程序很简单,它通过USB接收串行数据并将其打印在TK窗口中。它可以正常工作很长时间,但是如果停顿一天或一整夜都没有响应,并且我在顶部的应用程序栏上看到一般的窗口(没有响应)错误,如果我尝试最小化或关闭窗口,可能会最多需要5〜10分钟。

我在python终端窗口上没有任何错误

我已将计算机上的电池和电源设置更改为无法睡眠和正常性能,仍然无法解决问题

我已将代码剥离到最低限度,以查看它是否是解决问题的代码部分

下面是我的代码,希望有人可以帮我一下。

import tkinter as tk
from tkinter import *
from tkinter import ttk
import serial
import numpy 
import sys


arduinoData = serial.Serial('com7', 115200)  #Creating our serial object named arduinoData


# Main Tkinter application
class Application(Frame):

            # Init the variables & start measurements
        def __init__(self, master=None):
                Frame.__init__(self, master)
                root.title( "Dashboard")
                root.state('zoomed')

                self.grid(row=0, column=0, sticky="nsew")
                self.grid_rowconfigure(0, weight=1)
                self.grid_columnconfigure(0, weight=3)

                self.B1 = StringVar()

                self.createWidgets()
                self.pack()
                self.measure()

        # Create display elements
        def createWidgets(self):

                self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=0,padx=100,pady=200)

    # Read the incoming serial data and display it
        def measure(self):

                if(arduinoData.inWaiting()>0):                               #Wait till there is data to read

                        arduinoString = arduinoData.readline()               #read serial data
                        arduinoString =str(arduinoString,'utf-8')            #Removes the surrounding rubbish

                        self.B1.set(str(arduinoString))                      #Set the label to the received arduino data 
                        self.B1DO = Label(self, textvariable=self.B1, font=('Verdana', 15)).grid(row=0, column=0, sticky="nsew")

                arduinoData.flushOutput()                                    #Clear old data
                arduinoData.flushInput()

                self.after(1000,self.measure)                                #Wait 1 second between each measurement


# Create and run the GUI
root = Tk()
app = Application(master=root)
app.mainloop()

1 个答案:

答案 0 :(得分:5)

您似乎正在永久创建新的B1DO标签,这可能会在应用程序中造成资源泄漏。尝试采用self.B1DO定义并将其放在createWidgets中,以使标签仅创建一次:

import tkinter as tk
from tkinter import *
from tkinter import ttk
import serial
import numpy 
import sys


arduinoData = serial.Serial('com7', 115200)  #Creating our serial object named arduinoData


# Main Tkinter application
class Application(Frame):

            # Init the variables & start measurements
        def __init__(self, master=None):
                Frame.__init__(self, master)
                root.title( "Dashboard")
                root.state('zoomed')

                self.grid(row=0, column=0, sticky="nsew")
                self.grid_rowconfigure(0, weight=1)
                self.grid_columnconfigure(0, weight=3)

                self.B1 = StringVar()

                self.createWidgets()
                self.pack()
                self.measure()

        # Create display elements
        def createWidgets(self):
                self.B1DO = Label(self, textvariable=self.B1, font=('Verdana', 15)).grid(row=0, column=0, sticky="nsew")
                self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=0,padx=100,pady=200)

    # Read the incoming serial data and display it
        def measure(self):

                if(arduinoData.inWaiting()>0):                               #Wait till there is data to read

                        arduinoString = arduinoData.readline()               #read serial data
                        arduinoString =str(arduinoString,'utf-8')            #Removes the surrounding rubbish

                        self.B1.set(str(arduinoString))                      #Set the label to the received arduino data 


                arduinoData.flushOutput()                                    #Clear old data
                arduinoData.flushInput()

                self.after(1000,self.measure)                                #Wait 1 second between each measurement


# Create and run the GUI
root = Tk()
app = Application(master=root)
app.mainloop()