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

以下代码为python3版本,python2版本使用需保证更新数据的那部分代码处于主进程中,如果放在线程中会出现错误,所以建议能用 python3就用python3,只用我为什么知道python2会出现这个问题,那就是ros没python3的版本…

示意图(数据就会在后面的框里面通过程序运行时的值的改变而改变):
python使用tkinter编写一个数据显示窗口(实时显示更新的数据)
以下为代码,我这里在下面提供两个代码示例:

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入门项目】使用 Tkinter 的 Python 日历 GUI 应用程序


下一篇:python tkinter界面可视化工具