如何在PythonTkinterGUI中显示一些测量结果-每秒钟显示一个新值
我试图通过传感器测量一些值,并使用python tkinter GUI显示它。我设法创建了一个GUI和要测量的程序。现在我想在GUI中显示数据,当我每秒都得到新值时,我想用新值更新屏幕。我看到了无法使用的如何在PythonTkinterGUI中显示一些测量结果-每秒钟显示一个新值,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我试图通过传感器测量一些值,并使用python tkinter GUI显示它。我设法创建了一个GUI和要测量的程序。现在我想在GUI中显示数据,当我每秒都得到新值时,我想用新值更新屏幕。我看到了无法使用的textvariable。但是如何初始化呢?当程序启动时,由于开始时没有任何输出,因此显示错误。如何管理它。有什么建议吗。最后,如何每秒更新 from tkinter import * import tkinter.font import numpy as np import pigpio w
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()
您可以使用类来访问父结构中的所有数据。您需要初始化一个函数来调用另一个函数,该函数首先包含after属性。它只需在先计数1000毫秒,然后计数20毫秒后,一次又一次地配置数据(调整它以平滑地读取数据)。您可以添加更多动态数据/文本并使用“after”属性一次,它仍将刷新所有这些数据/文本。它是一种依赖于事件的解决方案,在数据读取方面明显优于依赖于时间或基于循环的刷新算法
import time
from random import random
from random import seed
from tkinter import StringVar,Entry
import tkinter as tk
class GUI:
#------------------INITIAL---------------------------------------------------------------------------------------------------------
def __init__(self, parent):
self.labelBackground = tk.Label(parent, text="",bg="white",width=1920,height=1080)
self.labelBackground.place(x=0,y=0)
self.labelDate = tk.Label(parent, text="Date",bg="white", font="Arial 20", width=100)
self.labelDate.pack()
self.labelDate.after(1000, self.refresh_label)
def dateData(self):
year,mon,day,hour,min,sec,a,b,c = time.localtime()
infoDate = f"Date/Time: {year} {mon} {day} h:{hour} m:{min} s:{sec} "
return f"Clock: {hour}:{min}:{sec} "
def refresh_label(self):
self.seconds = self.dateData()
self.labelDate.configure(text=self.seconds)
self.labelDate.after(20, self.refresh_label)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("1920x1080") #Window Resolution
root.title("Insert_Window_Title")
timer = GUI(root)
root.mainloop()
非常感谢。它是有效的:)。还有一个疑问。是否可以在tkinter中添加开始按钮和停止按钮来启动和停止测量?`startButton=button(win,text=“start”)')和一些启动测量的功能?欢迎使用。是的,这是可能的;我建议你尝试一下,如果你遇到困难,请随便问一个新问题。类似这样的内容:
start\u button=tk.button(win,text=“start”,command=start\u recording\u measures)