Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter应用程序随着时间的推移,响应速度越来越慢_Python_Tkinter - Fatal编程技术网

Python Tkinter应用程序随着时间的推移,响应速度越来越慢

Python Tkinter应用程序随着时间的推移,响应速度越来越慢,python,tkinter,Python,Tkinter,我的TKinter应用程序在长时间运行时出现问题。该应用程序很简单,它通过usb接收串行数据并在TK窗口上打印。它工作时间很长,但当一天或一夜离开hald时,它没有响应,我在顶部的应用程序栏上看到通用窗口(没有响应)错误,如果我尝试最小化或关闭窗口,可能需要5~10分钟 我没有在python终端窗口上得到任何错误 我已将电脑上的电池和电源设置更改为不睡眠和正常性能,但问题仍未解决 我已经将我的代码剥离到最低限度,看看它是否是引起问题的代码部分 下面是我的代码,希望有人能帮我解释一下 import

我的TKinter应用程序在长时间运行时出现问题。该应用程序很简单,它通过usb接收串行数据并在TK窗口上打印。它工作时间很长,但当一天或一夜离开hald时,它没有响应,我在顶部的应用程序栏上看到通用窗口(没有响应)错误,如果我尝试最小化或关闭窗口,可能需要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()

看起来您一直在创建新的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()

谢谢你的建议,我将在晚上尝试一下,我会让你知道结果。好的,现在一切正常,我已将此标记为已解决,并再次感谢你抽出时间。