gpt4 book ai didi

python - 随着时间的推移,Tkinter 应用程序的响应速度变得越来越慢

转载 作者:行者123 更新时间:2023-11-28 21:35:20 31 4
gpt4 key购买 nike

我的 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()

最佳答案

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

关于python - 随着时间的推移,Tkinter 应用程序的响应速度变得越来越慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52351441/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com