每当有来自串行端口的新数据时,从串行数据更新 tkinter 标签 python 3.x [英] update tkinter label from serial data whenever there's new data from serial port python 3.x

查看:37
本文介绍了每当有来自串行端口的新数据时,从串行数据更新 tkinter 标签 python 3.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过这个问题,我无法在标签上显示任何值,每当有新数据从串行端口进入时,我想不断更新它.我是 Python 新手,真的需要帮助.

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

推荐答案

您可以在 _thread 模块的独立线程中运行您的 GetTemp() 方法.使用 Tkinter 方法 after() 调用线程.在以下示例中,我将您的 GetTemp() 替换为生成的随机数.

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

这篇关于每当有来自串行端口的新数据时,从串行数据更新 tkinter 标签 python 3.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆