3

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()
4

2 回答 2

1

您可以在模块GetTemp()的单独线程中运行您的方法。使用方法_thread调用线程。在以下示例中,我将您替换为生成的随机数。Tkinterafter()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()
于 2013-07-04T07:27:20.120 回答
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()

于 2019-12-28T04:34:52.367 回答