如何在python tkinter中显示一些测量结果

时间:2019-07-02 08:42:38

标签: python python-3.x tkinter

我正在尝试使用传感器测量一些值,并使用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()

2 个答案:

答案 0 :(得分:1)

您需要使用最近读取的传感器值更新设置为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()

答案 1 :(得分:0)

您可以使用“类”从父结构访问所有数据。您需要初始化一个函数,以首先在内部赞扬之后调用另一个函数。在先计数1000毫秒然后计数20毫秒之后,它简单地一次又一次地配置数据(将其调整为平稳读取数据)。您可以添加更多动态数据/文本,并使用“之后”贡品一次,它仍将刷新所有这些信息。它是一种基于事件的解决方案,它明显优于基于时间或基于循环的刷新算法来读取数据。

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()