[英]update tkinter label from serial data whenever there's new data from serial port python 3.x
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.
我是 python 新手,真的需要帮助。
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()
You can run your GetTemp()
method in a seperated thread from _thread
module.您可以在与
_thread
模块分离的线程中运行GetTemp()
方法。 The thread is called with the Tkinter
method after()
.该线程使用
Tkinter
方法after()
调用。 In following example I substituted your GetTemp()
with a randomn number generated.在以下示例中,我用生成的随机数替换了您的
GetTemp()
。
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()
call serial inside the method..it will work..here is my code for serial using Mr Holger's concept.. this code is tested with Raspberry Pi3 serial port ttyS0 as master and serially connected sensor.....在方法内调用串行..它会工作..这是我使用霍尔格先生概念的串行代码..此代码使用Raspberry Pi3串行端口ttyS0作为主设备和串行连接的传感器进行测试.....
import tkinter
import tkinter.messagebox
import time
import _thread
import serial
class Menu:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Serial Data monitor")
self.main_window.geometry("1000x600")
#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)
#RPM
self.GetTempLabel = tkinter.Label(self.frame_2,\
text='RPM :')
self.TempValue = tkinter.StringVar()
self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
textvariable = self.TempValue
)
#torque
self.GetTemppLabel = tkinter.Label(self.frame_2,\
text='TORQUE :')
self.TemppValue = tkinter.StringVar()
self.GetTemppValueLabel = 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)
#rpm
self.GetTempLabel.pack()
self.GetTempLabel.place(x=50, y=80, height=20, width=80)
self.GetTempValueLabel.pack()
self.GetTempValueLabel.place(x=200, y=80, height=20, width=120)
#torque
self.GetTemppLabel.pack()
self.GetTemppLabel.place(x=50, y=120, height=20, width=80)
self.GetTemppValueLabel.pack()
self.GetTemppValueLabel.place(x=200, y=120, height=20, width=120)
#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, ())
self.main_window.after(4000, _thread.start_new_thread, self.GetTempp, ())
tkinter.mainloop()
def GetTemp(self):
s=serial.Serial(port='/dev/ttyS0',baudrate=9600)
string='*00T%'
while True:
s.write(str.encode(string))
print(string)
time.sleep(2)
if s.inWaiting():
temp=s.readline(s.inWaiting())
value=temp.decode('utf-8')
value=value[5:-1]
def GetTempp(self):
s=serial.Serial(port='/dev/ttyS0',baudrate=9600)
string1='*01T%'
while True:
s.write(str.encode(string1))
print(string1)
time.sleep(2)
if s.inWaiting():
tempp=s.readline(s.inWaiting())
value1=tempp.decode('utf-8')
value1=value1[5:-1]
print(value1)
self.TemppValue.set(str(value1))
time.sleep(0.5)
gui = Menu()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.