【问题标题】:How to display some measured result in python tkinter GUi- new values every seconds如何在python tkinter GUI中显示一些测量结果 - 每秒新值
【发布时间】:2019-07-02 08:42:38
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案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()
    

    【讨论】:

    • 谢谢。有用 :)。还有一个疑问。是否可以在 tkinter 中添加开始按钮和停止按钮来开始和停止测量?
    • `startButton = Button(win, text = "Start")´ 和一些开始测量的函数?
    • 不客气。对的,这是可能的;我建议您尝试一下,如果您遇到困难,请随时提出新问题。像这样:start_button = tk.Button(win, text="Start", command=start_recording_measurements)
    • 你能看看这个链接吗 --> stackoverflow.com/q/56937694/11372345
    【解决方案2】:

    您可以使用类来访问父结构中的所有数据。您需要先初始化一个函数以在其中调用另一个函数。它只是在先计数 1000 ms 再计数 20 ms 后一次又一次地配置数据(调整它以平滑读取数据)。您可以添加更多动态数据/文本并使用“之后”致敬一次,它仍然会刷新所有这些。它是一种事件相关解决方案,在数据读取方面明显优于时间相关或基于循环的刷新算法。

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

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多