我遇到过这个问题,我无法在标签上显示任何值,只要有来自串口的新数据,我就会不断更新它。我是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()
答案 0 :(得分:0)
您可以在GetTemp()
模块的单独线程中运行_thread
方法。使用Tkinter
方法after()
调用该线程。在下面的示例中,我将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()
答案 1 :(得分:0)
在方法内部调用serial ..它将起作用..这是我使用Holger先生的概念编写的代码。 此代码已通过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()