我在运行很长一段时间后使用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()
Run Code Online (Sandbox Code Playgroud)
您似乎永远在创建新的B1DO标签,这可能会在您的应用程序中造成资源泄漏.尝试获取self.B1DO定义并将其放入,createWidgets以便仅创建一次Label:
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()
Run Code Online (Sandbox Code Playgroud)