问题
I have encounter this problem where i could not display any value on the label which i wanted to constantly update it whenever there's new data coming in from the serial port. I'm new to python, really need the help.
import tkinter
import tkinter.messagebox
import serial
import time
ser = serial.Serial(
port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=4)
class Menu:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Room Light System")
self.main_window.geometry("1200x600")
#Frames
self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs
#ReceiveLabel
self.ReceiveLabel = tkinter.Label(self.frame_2,\
text = 'Received DATAs',\
bg = 'White',\
height = 2, width = 20)
#Temperature
self.GetTempLabel = tkinter.Label(self.frame_2,\
text='Temperature :')
self.TempValue = tkinter.StringVar()
self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
textvariable = self.TempValue
)
#PACKING!!! F2
self.frame_2.pack()
self.frame_2.place(x=410, y=0, height=300, width=400)
#ReceiveLabel
self.ReceiveLabel.pack()
self.ReceiveLabel.place(x=100, y=10)
#Temperature
self.GetTempLabel.pack()
self.GetTempLabel.place(x=50, y=80, height=20, width=120)
self.GetTempValueLabel.pack()
self.GetTempValueLabel.place(x=200, y=80, height=20, width=50)
#main loop and quit
self.quitButton = tkinter.Button(self.main_window,\
text = 'Quit',
command = self.main_window.destroy,\
height = 2, width = 6)
self.quitButton.pack()
self.quitButton.place(x=200, y=500)
tkinter.mainloop()
def GetTemp(self):
data = bytearray()
while(1):
readline = ser.read(size=10)
if len(readline) > 0 :
data = readline
v = memoryview(data)
P = v.tobytes()
P = P.decode(encoding='UTF-8')
self.TempValue.set(P)
gui = Menu()
ser.close()
回答1:
You can run your GetTemp()
method in a seperated thread from _thread
module. The thread is called with the Tkinter
method after()
. In following example I substituted your GetTemp()
with a randomn number generated.
import tkinter
import tkinter.messagebox
import time
import random
import _thread
class Menu:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Room Light System")
self.main_window.geometry("1200x600")
#Frames
self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs
#ReceiveLabel
self.ReceiveLabel = tkinter.Label(self.frame_2,\
text = 'Received DATAs',\
bg = 'White',\
height = 2, width = 20)
#Temperature
self.GetTempLabel = tkinter.Label(self.frame_2,\
text='Temperature :')
self.TempValue = tkinter.StringVar()
self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
textvariable = self.TempValue
)
#PACKING!!! F2
self.frame_2.pack()
self.frame_2.place(x=410, y=0, height=300, width=400)
#ReceiveLabel
self.ReceiveLabel.pack()
self.ReceiveLabel.place(x=100, y=10)
#Temperature
self.GetTempLabel.pack()
self.GetTempLabel.place(x=50, y=80, height=20, width=120)
self.GetTempValueLabel.pack()
self.GetTempValueLabel.place(x=200, y=80, height=20, width=50)
#main loop and quit
self.quitButton = tkinter.Button(self.main_window,\
text = 'Quit',
command = self.main_window.destroy,\
height = 2, width = 6)
self.quitButton.pack()
self.quitButton.place(x=200, y=500)
self.main_window.after(2000, _thread.start_new_thread, self.GetTemp, ())
tkinter.mainloop()
def GetTemp(self):
while(1):
value = random.random()
self.TempValue.set(str(value))
time.sleep(0.5)
gui = Menu()
来源:https://stackoverflow.com/questions/17463521/update-tkinter-label-from-serial-data-whenever-theres-new-data-from-serial-port