python - 如何在 python tkinter GUIi 中显示一些测量结果 - 每秒新值
问题描述
我正在尝试使用传感器测量一些值并使用 python tkinter GUI 显示它。我设法创建了一个 GUI 和要测量的程序。现在我想在 GUI 中显示数据,因为我每秒都在获取新值,所以我想用新值更新屏幕。我见过比我能用textvariable
的。但是如何初始化呢?程序启动时,由于一开始没有任何输出,所以显示错误。如何管理它。请有任何建议。最后如何每秒更新?
from tkinter import *
import tkinter.font
import numpy as np
import pigpio
win = Tk()
myFont = tkinter.font.Font(family = 'Verdana', size = 20, weight = 'bold')
win.geometry('800x480')
win.configure(background='#CD5C5C')
#------------------------------------------------------main program ----------------------------------#
def readSensors():
#function body
# output is a list with name measuredValues
#measuredValues contains total 4 values as I have 4 sensors
win.after(1000, readSensors) #calling the function every second
#label names variable
output_1= StringVar()
output_2 = StringVar()
output_3 = StringVar()
output_4 = StringVar()
value0 = str(measuredValues[0])
value1= str(measuredValues[1])
value2 = str(measuredValues[2])
value3 = str(measuredValues[3])
output_1.set (value0)
output_2.set (value1)
output_3.set (value2)
output_4.set(value3)
#Labels
# i used textvariable to to measured values. but doesn't work so far
#display values
output_1_label = Label(win, textvariable = output_1,height =2, width = 12)
output_1_label.place(x=200, y=100)
output_2_label = Label(win, textvariable = output_2, height =2, width = 12)
output_2_label.place(x=200, y=200)
output_3_label = Label(win, textvariable = output_3,height =2, width = 12)
output_3_label.place(x=200, y=300)
output_4_label = Label(win, textvariable = output_4, height =2, width = 12)
output_4_label.place(x=200, y=400)
#how to update the window with new data?
win.after(1000, readSensor)
win.mainloop()
解决方案
您需要textvariables
使用上次读取的传感器值更新变量集:
像这样 - 传感器读数被随机选择的值替换以模拟新的数据读数:
import tkinter as tk
import random
def readSensors():
output_1.set(random.choice([0, 1, 2, 3, 4, 5]))
output_2.set(random.choice([0, 1, 2, 3, 4, 5]))
output_3.set(random.choice([0, 1, 2, 3, 4, 5]))
output_4.set(random.choice([0, 1, 2, 3, 4, 5]))
win.after(1000, readSensors)
win = tk.Tk()
win.geometry('800x480')
win.configure(background='#CD5C5C')
output_1 = tk.StringVar()
output_2 = tk.StringVar()
output_3 = tk.StringVar()
output_4 = tk.StringVar()
measuredValues = [0, 1, 2, 3, 4, 5]
value0 = str(measuredValues[0])
value1 = str(measuredValues[1])
value2 = str(measuredValues[2])
value3 = str(measuredValues[3])
output_1.set(value0)
output_2.set(value1)
output_3.set(value2)
output_4.set(value3)
output_1_label = tk.Label(win, textvariable=output_1, height=2, width=12)
output_1_label.place(x=200, y=100)
output_2_label = tk.Label(win, textvariable=output_2, height=2, width=12)
output_2_label.place(x=200, y=200)
output_3_label = tk.Label(win, textvariable=output_3, height=2, width=12)
output_3_label.place(x=200, y=300)
output_4_label = tk.Label(win, textvariable=output_4, height=2, width=12)
output_4_label.place(x=200, y=400)
win.after(1000, readSensors)
win.mainloop()
推荐阅读
- .net - .Net 控制方法 GetChildIndex 和 IndexOf 有什么区别
- python - Django reverse 寻找错误的视图功能
- r - 想要从 R Studio 中巨大 JSONL 文件的每一行输出两个值
- python - 写入文件时如何防止lxml更改空元素?
- php - 如果字符串包含数字字符,则 PhpUnit 断言
- excel - 在用户窗体中更改时如何重新启动宏
- nativescript - 更新到 iOs 12(NativeScript Vanilla)后无法在设备上安装
- java - 给定一个数字数组,返回所有其他数字的乘积数组而不进行除法?
- mysql - 在mysql表中添加一个带有随机时间戳的列
- html - CSS网格列拉伸以填充整行/或居中行?