python使用tkinter编写一个数据显示窗口(实时显示更新的数据)
2021/12/31 17:07:15
本文主要是介绍python使用tkinter编写一个数据显示窗口(实时显示更新的数据),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
以下代码为python3版本,python2版本使用需保证更新数据的那部分代码处于主进程中,如果放在线程中会出现错误,所以建议能用 python3就用python3,只用我为什么知道python2会出现这个问题,那就是ros没python3的版本…
示意图(数据就会在后面的框里面通过程序运行时的值的改变而改变):
以下为代码,我这里在下面提供两个代码示例:
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编写一个数据显示窗口(实时显示更新的数据)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享