python使用tkinter编写一个数据显示窗口(实时显示更新的数据)

以下代码分为python3版本与python2版本,

python3版本主要靠
self.main_window.after( 刷新的时间毫秒, 需要刷新的函数这里为窗口的清除与插入函数) 刷新函数进行 ,

python2版本则通过编写一个线程进行刷新作用,刷新函数在python2 环境下会报错…
至于我为什么知道python2会出现这个问题,那就是ros没python3的版本…

示意图

(数据就会在后面的框里面通过程序运行时的值的改变而改变):

python使用tkinter编写一个数据显示窗口(实时显示更新的数据)_第1张图片
python3版本

不解释了直接可以运行,运行效果为不断的刷新一号测距仪的值
python使用tkinter编写一个数据显示窗口(实时显示更新的数据)_第2张图片

#!/usr/bin/env python2.7
# coding:utf-8
# -*- coding: utf-8 -*-

"""通过开启tcp客户端进行接收,更新下位机的服务端更新的数据"""

import tkinter

class Status():
    """解析的依据在于,status键对应的标识符,具体的参考word手册"""
    def __init__(self):


        self.json_data = {} #
        self.odom()


    def odom(self):
        "用于储存小车底盘的,里程计,瞬时速度等信息"
        self.x = -0.99
        self.y = -0.99
        self.p = -0.99
        self.vx = -0.99
        self.vy = -0.99
        self.vp = -0.99
        self.s_x = -0.99
        self.s_y = -0.99
        self.s_p = -0.99


    # def save_judge_status(self,json_data):
    #     """对服务器传来的,单条json数据进行解析,判断是否符合,协议,并根据协议,取出对应 dict的有效值"""
    #     """若通过判断,则表示为无效量"""
    #     status_name =  json_data["status"]
    #     if status_name in ["Odom_m"]:
    #         #此时说明,传输正确  对应cs1


s_data = Status()



class MYIP():

    def __init__(self):
        print("开启显示窗口模块-----------------------")

        self.refresh_rate = 300
        # main frame
        self.main_window = tkinter.Tk()

        # top frame
        self.top_frame = tkinter.Frame(self.main_window)
        # bottom frame
        self.bottom_frame = tkinter.Frame(self.main_window)

        self.L1 = tkinter.Label(self.top_frame, text='底盘驱动')
        self.L1.pack(side='left')
        default_value = tkinter.StringVar()
        default_value.set('V3.1 python3(客户端)版本')
        self.E1 = tkinter.Entry(self.top_frame, textvariable=default_value, bd=2)
        self.E1.pack(side=tkinter.LEFT)

        self.bottom_left = tkinter.Frame(self.bottom_frame)
        self.bottom_right = tkinter.Frame(self.bottom_frame)

        self.label1 = tkinter.Label(self.bottom_left, text="%-10s" % "1号测距仪")
        self.label2 = tkinter.Label(self.bottom_left, text="%-10s" % '2号测距仪')
        self.label3 = tkinter.Label(self.bottom_left, text="%-10s" % '3号测距仪')
        self.label4 = tkinter.Label(self.bottom_left, text="%-10s" % '4号测距仪')
        self.label5 = tkinter.Label(self.bottom_left, text="%-10s" % '5号测距仪')
        self.label6 = tkinter.Label(self.bottom_left, text="%-10s" % '6号测距仪')

        self.label7 = tkinter.Label(self.bottom_left, text="%-10s" % '7号测距仪')
        self.label8 = tkinter.Label(self.bottom_left, text="%-10s" % '8号测距仪')
        self.label9 = tkinter.Label(self.bottom_left, text="%-10s" % '9号测距仪')
        self.label10 = tkinter.Label(self.bottom_left, text="%-10s" % '刷新率')

        self.label1.pack(side='top')
        self.label2.pack(side='top')
        self.label3.pack(side='top')
        self.label4.pack(side='top')
        self.label5.pack(side='top')
        self.label6.pack(side='top')
        self.label7.pack(side='top')
        self.label8.pack(side='top')
        self.label9.pack(side='top')
        self.label10.pack(side='top')

        self.En1 = tkinter.Entry(self.bottom_right)
        self.En2 = tkinter.Entry(self.bottom_right)
        self.En3 = tkinter.Entry(self.bottom_right)
        self.En4 = tkinter.Entry(self.bottom_right)
        self.En5 = tkinter.Entry(self.bottom_right)
        self.En6 = tkinter.Entry(self.bottom_right)

        self.En7 = tkinter.Entry(self.bottom_right)
        self.En8 = tkinter.Entry(self.bottom_right)
        self.En9 = tkinter.Entry(self.bottom_right)
        self.En10 = tkinter.Entry(self.bottom_right)

        ret = ("label1", "label2", "label3", "label4", "label5", "label6")
        self.En1.delete(0, tkinter.END)
        self.En2.delete(0, tkinter.END)
        self.En3.delete(0, tkinter.END)
        self.En4.delete(0, tkinter.END)
        self.En5.delete(0, tkinter.END)
        self.En6.delete(0, tkinter.END)
        self.En1.insert(0, ret[0])
        self.En2.insert(0, ret[1])
        self.En3.insert(0, ret[2])
        self.En4.insert(0, ret[3])
        self.En5.insert(0, ret[4])
        self.En6.insert(0, ret[5])

        self.En1.pack(side='top')
        self.En2.pack(side='top')
        self.En3.pack(side='top')
        self.En4.pack(side='top')
        self.En5.pack(side='top')
        self.En6.pack(side='top')

        self.En7.pack(side='top')
        self.En8.pack(side='top')
        self.En9.pack(side='top')
        self.En10.pack(side='top')

        self.bottom_left.pack(side='left')
        self.bottom_right.pack(side='right')
        self.top_frame.pack()
        self.bottom_frame.pack()

        self.c_1 = -1
        self.c_2 = -1
        self.c_3 = -1
        self.c_4 = -1
        self.c_5 = -1
        self.c_6 = -1
        self.c_7 = -1
        self.c_8 = -1
        self.c_9 = -1
        self.ref_num = -1
        self.main_window.after(100, self.updata_node)
        self.main_window.mainloop()

    def updata_node(self):
        print("刷新一次")

        self.updata_xyp()
        # 接下来刷新页面
        self.En1.delete(0, 10)
        self.En1.insert(0, str(self.c_1))  # delete
        self.En1.update()

        self.En2.delete(0, 10)
        self.En2.insert(0, str(self.c_2))  # delete
        self.En2.update()

        self.En3.delete(0, 10)
        self.En3.insert(0, str(self.c_3))  # delete
        self.En3.update()

        self.En4.delete(0, 10)
        self.En4.insert(0, str(self.c_4))  # delete
        self.En4.update()

        self.En5.delete(0, 10)
        self.En5.insert(0, str(self.c_5))  # delete
        self.En5.update()

        self.En6.delete(0, 10)
        self.En6.insert(0, str(self.c_6))  # delete
        self.En6.update()

        self.En7.delete(0, 10)
        self.En7.insert(0, str(self.c_7))  # delete
        self.En7.update()

        self.En8.delete(0, 10)
        self.En8.insert(0, str(self.c_8))  # delete
        self.En9.update()

        self.En9.delete(0, 10)
        self.En9.insert(0, str(self.c_9))  # delete
        self.En9.update()

        self.En10.delete(0, 10)
        self.En10.insert(0, str(self.ref_num))  # delete
        self.En10.update()

        self.main_window.after(self.refresh_rate, self.updata_node)

    def updata_xyp(self):
        """通过客户端连接到下位机服务端收到的数据更新"""
        print("4564 : " + str(s_data.x))
        s_data.x = s_data.x + 1
        self.c_1 = s_data.x
        # self.y = s_data.y
        # self.w = s_data.p


if __name__ == '__main__':
    # c = Client_link()
    # c.start_client()
    # print("开启tcp客户端线程拉起完成")

    m_win = MYIP()








python2版本

以下为代码,我这里在下面提供两个代码示例:

1.(第一部分)修改为可以直接运行显示,但注释掉了刷新数据的线程部分的,无法对数据进行更新。

"""注意这个 窗口拉起接口在 一 二 部分中被调用的不同的写法"""
self.main_window.mainloop()

2.(第二部分)没有注释掉线程部分,需要通过 from import 的方法在另外一个py文件的线程中进行调用更新,下面是我截取的部分在另一个py文件中的调用,示例

from hint_window import m_win


 def t_correct(self):
        t_correct = threading.Thread(target=self.calculate_instant_speed)
        t_correct.setDaemon(True)
        t_correct.start()
  def calculate_instant_speed(self):
        """用于更新瞬时速度"""
        """
        self.x_speed ,self.y_speed, self.p_speed
        """
        

            m_win.updata_node("vx", self.x_speed)
            m_win.updata_node("vy", self.y_speed)
            m_win.updata_node("vw", self.p_speed)


(第一部分):

#!/usr/bin/env python2.7
#coding:utf-8
# -*- coding: utf-8 -*-
import tkinter
# import ctypes
import time
import threading

class MYIP(threading.Thread):

    def __init__(self):
        # 隐藏窗口
        # whnd = ctypes.windll.kernel32.GetConsoleWindow()
        # if whnd != 0:
        #     ctypes.windll.user32.ShowWindow(whnd, 0)
        #     ctypes.windll.kernel32.CloseHandle(whnd)



        # main frame
        self.main_window = tkinter.Tk()

        # top frame
        self.top_frame = tkinter.Frame(self.main_window)
        # bottom frame
        self.bottom_frame = tkinter.Frame(self.main_window)

        self.L1 = tkinter.Label(self.top_frame, text='底盘驱动')
        self.L1.pack(side='left')
        default_value = tkinter.StringVar()
        default_value.set('V2.1 python2版本')
        self.E1 = tkinter.Entry(self.top_frame, textvariable=default_value, bd=2)
        self.E1.pack(side=tkinter.LEFT)

        self.bottom_left = tkinter.Frame(self.bottom_frame)
        self.bottom_right = tkinter.Frame(self.bottom_frame)

        self.label1 = tkinter.Label(self.bottom_left, text="%-10s" % "X(mm)")
        self.label2 = tkinter.Label(self.bottom_left, text="%-10s" % 'Y(mm)')
        self.label3 = tkinter.Label(self.bottom_left, text="%-10s" % 'W(度)')
        self.label4 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vx(m/s)')
        self.label5 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vy(m/s)')
        self.label6 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vw(rad/s)')

        self.label7 = tkinter.Label(self.bottom_left, text="%-10s" % 'C_x')
        self.label8 = tkinter.Label(self.bottom_left, text="%-10s" % 'C_y')
        self.label9 = tkinter.Label(self.bottom_left, text="%-10s" % 'C_w')

        self.label1.pack(side='top')
        self.label2.pack(side='top')
        self.label3.pack(side='top')
        self.label4.pack(side='top')
        self.label5.pack(side='top')
        self.label6.pack(side='top')
        self.label7.pack(side='top')
        self.label8.pack(side='top')
        self.label9.pack(side='top')

        self.En1 = tkinter.Entry(self.bottom_right)
        self.En2 = tkinter.Entry(self.bottom_right)
        self.En3 = tkinter.Entry(self.bottom_right)
        self.En4 = tkinter.Entry(self.bottom_right)
        self.En5 = tkinter.Entry(self.bottom_right)
        self.En6 = tkinter.Entry(self.bottom_right)

        self.En7 = tkinter.Entry(self.bottom_right)
        self.En8 = tkinter.Entry(self.bottom_right)
        self.En9 = tkinter.Entry(self.bottom_right)

        ret = ("label1", "label2", "label3", "label4", "label5", "label6")
        self.En1.delete(0, tkinter.END)
        self.En2.delete(0, tkinter.END)
        self.En3.delete(0, tkinter.END)
        self.En4.delete(0, tkinter.END)
        self.En5.delete(0, tkinter.END)
        self.En6.delete(0, tkinter.END)
        self.En1.insert(0, ret[0])
        self.En2.insert(0, ret[1])
        self.En3.insert(0, ret[2])
        self.En4.insert(0, ret[3])
        self.En5.insert(0, ret[4])
        self.En6.insert(0, ret[5])

        self.En1.pack(side='top')
        self.En2.pack(side='top')
        self.En3.pack(side='top')
        self.En4.pack(side='top')
        self.En5.pack(side='top')
        self.En6.pack(side='top')

        self.En7.pack(side='top')
        self.En8.pack(side='top')
        self.En9.pack(side='top')

        self.bottom_left.pack(side='left')
        self.bottom_right.pack(side='right')
        self.top_frame.pack()
        self.bottom_frame.pack()
        print("准备开启窗口数据更新线程")
        self.main_window.mainloop()

        # t_updata = threading.Thread(target=self.updata_datas)
        # t_updata.start()




    def updata_datas(self):
        while True:
            # self.label1 = tkinter.Label(self.bottom_left, text="%-10s" % "X(mm)")
            # self.label2 = tkinter.Label(self.bottom_left, text="%-10s" % 'Y(mm)')
            # self.label3 = tkinter.Label(self.bottom_left, text="%-10s" % 'W(度)')
            # self.label4 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vx(m/s)')
            # self.label5 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vy(m/s)')
            # self.label6 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vw(rad/s)')
            print (110)
            time.sleep(0.2)
            self.main_window.mainloop()
    
    def updata_node(self,name,val):
        if name == "x":
            self.En1.delete(0, 10)
            self.En1.insert(0, str(val))  # delete
        elif name == "y":
            self.En2.delete(0, 10)
            self.En2.insert(0, str(val))  # delete
        elif name == "w":
            self.En3.delete(0, 10)
            self.En3.insert(0, str(val))  # delete
        elif name == "vx":
            self.En4.delete(0, 10)
            self.En4.insert(0, str(val))  # delete
        elif name == "vy":
            self.En5.delete(0, 10)
            self.En5.insert(0, str(val))  # delete
        elif name == "vw":
            self.En6.delete(0, 10)
            self.En6.insert(0, str(val))  # delete
        elif name == "cx":
            self.En7.delete(0, 10)
            self.En7.insert(0, str(val))  # delete
        elif name == "cy":
            self.En8.delete(0, 10)
            self.En8.insert(0, str(val))  # delete
        elif name == "cw":
            self.En9.delete(0, 10)
            self.En9.insert(0, str(val))  # delete





    def process_data(self):
        if self.jsondata['data']['country']:
            country = self.jsondata['data']['country']
        else:
            country = "NULL"

        if self.jsondata['data']['area']:
            area = self.jsondata['data']['area']
        else:
            area = "NULL"

        if self.jsondata['data']['region']:
            region = self.jsondata['data']['region']
        else:
            region = "NULL"

        if self.jsondata['data']['city']:
            city = self.jsondata['data']['city']
        else:
            city = "NULL"

        if self.jsondata['data']['county']:
            county = self.jsondata['data']['county']
        else:
            county = "NULL"

        if self.jsondata['data']['isp']:
            isp = self.jsondata['data']['isp']
        else:
            isp = "NULL"
        return (country, area, region, city, county, isp)



m_win = MYIP()

# if __name__ == '__main__':
#
#     m_win = MYIP()

(第二部分):

#!/usr/bin/env python2.7
# coding:utf-8
# -*- coding: utf-8 -*-
import tkinter
# import ctypes
import time
import threading


class MYIP(threading.Thread):

    def __init__(self):
        # 隐藏窗口
        # whnd = ctypes.windll.kernel32.GetConsoleWindow()
        # if whnd != 0:
        #     ctypes.windll.user32.ShowWindow(whnd, 0)
        #     ctypes.windll.kernel32.CloseHandle(whnd)

        # main frame
        self.main_window = tkinter.Tk()

        # top frame
        self.top_frame = tkinter.Frame(self.main_window)
        # bottom frame
        self.bottom_frame = tkinter.Frame(self.main_window)

        self.L1 = tkinter.Label(self.top_frame, text='底盘驱动')
        self.L1.pack(side='left')
        default_value = tkinter.StringVar()
        default_value.set('V2.1 python2版本')
        self.E1 = tkinter.Entry(self.top_frame, textvariable=default_value, bd=2)
        self.E1.pack(side=tkinter.LEFT)

        self.bottom_left = tkinter.Frame(self.bottom_frame)
        self.bottom_right = tkinter.Frame(self.bottom_frame)

        self.label1 = tkinter.Label(self.bottom_left, text="%-10s" % "X(mm)")
        self.label2 = tkinter.Label(self.bottom_left, text="%-10s" % 'Y(mm)')
        self.label3 = tkinter.Label(self.bottom_left, text="%-10s" % 'W(度)')
        self.label4 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vx(m/s)')
        self.label5 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vy(m/s)')
        self.label6 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vw(rad/s)')

        self.label7 = tkinter.Label(self.bottom_left, text="%-10s" % 'C_x')
        self.label8 = tkinter.Label(self.bottom_left, text="%-10s" % 'C_y')
        self.label9 = tkinter.Label(self.bottom_left, text="%-10s" % 'C_w')

        self.label1.pack(side='top')
        self.label2.pack(side='top')
        self.label3.pack(side='top')
        self.label4.pack(side='top')
        self.label5.pack(side='top')
        self.label6.pack(side='top')
        self.label7.pack(side='top')
        self.label8.pack(side='top')
        self.label9.pack(side='top')

        self.En1 = tkinter.Entry(self.bottom_right)
        self.En2 = tkinter.Entry(self.bottom_right)
        self.En3 = tkinter.Entry(self.bottom_right)
        self.En4 = tkinter.Entry(self.bottom_right)
        self.En5 = tkinter.Entry(self.bottom_right)
        self.En6 = tkinter.Entry(self.bottom_right)

        self.En7 = tkinter.Entry(self.bottom_right)
        self.En8 = tkinter.Entry(self.bottom_right)
        self.En9 = tkinter.Entry(self.bottom_right)

        ret = ("label1", "label2", "label3", "label4", "label5", "label6")
        self.En1.delete(0, tkinter.END)
        self.En2.delete(0, tkinter.END)
        self.En3.delete(0, tkinter.END)
        self.En4.delete(0, tkinter.END)
        self.En5.delete(0, tkinter.END)
        self.En6.delete(0, tkinter.END)
        self.En1.insert(0, ret[0])
        self.En2.insert(0, ret[1])
        self.En3.insert(0, ret[2])
        self.En4.insert(0, ret[3])
        self.En5.insert(0, ret[4])
        self.En6.insert(0, ret[5])

        self.En1.pack(side='top')
        self.En2.pack(side='top')
        self.En3.pack(side='top')
        self.En4.pack(side='top')
        self.En5.pack(side='top')
        self.En6.pack(side='top')

        self.En7.pack(side='top')
        self.En8.pack(side='top')
        self.En9.pack(side='top')

        self.bottom_left.pack(side='left')
        self.bottom_right.pack(side='right')
        self.top_frame.pack()
        self.bottom_frame.pack()
        print("准备开启窗口数据更新线程")
        # self.main_window.mainloop()

        t_updata = threading.Thread(target=self.updata_datas)
        t_updata.start()

    def updata_datas(self):
        while True:
            # self.label1 = tkinter.Label(self.bottom_left, text="%-10s" % "X(mm)")
            # self.label2 = tkinter.Label(self.bottom_left, text="%-10s" % 'Y(mm)')
            # self.label3 = tkinter.Label(self.bottom_left, text="%-10s" % 'W(度)')
            # self.label4 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vx(m/s)')
            # self.label5 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vy(m/s)')
            # self.label6 = tkinter.Label(self.bottom_left, text="%-10s" % 'Vw(rad/s)')
            print(110)
            time.sleep(0.2)
            self.main_window.mainloop()

    def updata_node(self, name, val):
        if name == "x":
            self.En1.delete(0, 10)
            self.En1.insert(0, str(val))  # delete
        elif name == "y":
            self.En2.delete(0, 10)
            self.En2.insert(0, str(val))  # delete
        elif name == "w":
            self.En3.delete(0, 10)
            self.En3.insert(0, str(val))  # delete
        elif name == "vx":
            self.En4.delete(0, 10)
            self.En4.insert(0, str(val))  # delete
        elif name == "vy":
            self.En5.delete(0, 10)
            self.En5.insert(0, str(val))  # delete
        elif name == "vw":
            self.En6.delete(0, 10)
            self.En6.insert(0, str(val))  # delete
        elif name == "cx":
            self.En7.delete(0, 10)
            self.En7.insert(0, str(val))  # delete
        elif name == "cy":
            self.En8.delete(0, 10)
            self.En8.insert(0, str(val))  # delete
        elif name == "cw":
            self.En9.delete(0, 10)
            self.En9.insert(0, str(val))  # delete

    def process_data(self):
        if self.jsondata['data']['country']:
            country = self.jsondata['data']['country']
        else:
            country = "NULL"

        if self.jsondata['data']['area']:
            area = self.jsondata['data']['area']
        else:
            area = "NULL"

        if self.jsondata['data']['region']:
            region = self.jsondata['data']['region']
        else:
            region = "NULL"

        if self.jsondata['data']['city']:
            city = self.jsondata['data']['city']
        else:
            city = "NULL"

        if self.jsondata['data']['county']:
            county = self.jsondata['data']['county']
        else:
            county = "NULL"

        if self.jsondata['data']['isp']:
            isp = self.jsondata['data']['isp']
        else:
            isp = "NULL"
        return (country, area, region, city, county, isp)


m_win = MYIP()

# if __name__ == '__main__':
#
#     m_win = MYIP()

你可能感兴趣的:(python小代码记录,python,开发语言,后端)