【问题标题】:Continous update of sensor data using while loop in python and tkinter在 python 和 tkinter 中使用 while 循环持续更新传感器数据
【发布时间】:2020-06-27 09:26:44
【问题描述】:

我正在使用 NI 仪器读取数据并将其显示在 GUI 上。用过tkinter。但是找不到使用 while 循环更新数据的方法。

import nidaqmx
import time
from tkinter import *
master = Tk()
while True:
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
        print('1 Channel 1 Sample Read: ')
        data = task.read()
        sensor_value ='%.2f' % data #said sensor value
        master.minsize(width=400, height=400)
        w = Label(master, text=sensor_value) #shows as text in the window
        w.pack() #organizes widgets in blocks before placing them in the parent.          
        time.sleep(5)
        mainloop()

【问题讨论】:

    标签: python tkinter while-loop tk nidaqmx


    【解决方案1】:

    使用 Tkinter 时我们应该避免线程,while 循环使用 root.update() 这不是我们不能使用它们但不建议使用 Tkinter 本身提供的 after(delay_ms, callback=None, *args) 方法是有原因的。

    现在到您的代码,您的代码中几乎没有问题。

    1. while 循环中,您每 5 秒创建一个 Label,而不是创建一个标签并使用 w['text'] = '...' 在循环内更新该标签的值w.configure(text='...')

    2. 不要将mainloop 放在循环中,而是在最后一行使用主窗口实例调用它master(@ 987654331@).

    3. master.minsize(width=400, height=400) 相同,您不必每 5 秒告诉主窗口将最小尺寸设置为 400x400,否则应该调用一次决定将最小尺寸更改为不同的几何形状。

    您的代码应如下所示。

    import nidaqmx
    from tkinter import *
    
    master = Tk()
    master.minsize(width=400, height=400)
    w = Label(master) #shows as text in the window
    w.pack() #organizes widgets in blocks before placing them in the parent.   
    
    def run():
        with nidaqmx.Task() as task:
            task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
            print('1 Channel 1 Sample Read: ')
            data = task.read()
            w['text'] = '%.2f' % data #said sensor value
        master.after(5000, run)
    
    run() # run the function once.
    master.mainloop()
    

    这应该是正确的做法,如果有任何问题,我无法运行您的代码,否则请告诉我。

    【讨论】:

      【解决方案2】:

      试试这个代码:

      import time
      from tkinter import *
      import nidaqmx
      
      master = Tk()
      
      def test():
          while True:
              with nidaqmx.Task() as task:
                  task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
                  print('1 Channel 1 Sample Read: ')
                  data = task.read()
                  sensor_value = '%.2f' % data  # said sensor value
                  
                  w['text'] = sensor_value
                  master.update()
                  time.sleep(2)
      
      w = Label(master)
      w.pack()
      btn = Button(text="Start read from sensor", command=test)
      btn.pack()
      mainloop()
      

      【讨论】:

        【解决方案3】:

        您可能需要第二个线程来轮询传感器。

        import nidaqmx
        import time
        import threading
        from tkinter import *
        
        
        stop_signal = threading.Event()
        
        
        def read_loop():
            with nidaqmx.Task() as task:
                task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
                while True:
                    data = task.read()
                    label["text"] = "%.2f" % data
        
                    # Wait for the signal, or 5 seconds
                    if stop_signal.wait(timeout=5):
                        break  # If the signal was set, break
        
        
        # Build window
        master = Tk()
        master.minsize(width=400, height=400)
        label = Label(master, text="")
        label.pack()
        
        # Set up & start reading thread
        threading.Thread(target=read_loop).start()
        
        try:
            # Enter Tk main loop
            mainloop()
        finally:
            # Clean up afterwards
            stop_signal.set()
        

        【讨论】:

          【解决方案4】:

          请记住,mainloop() 是阻塞的,所以time.sleep() 是无用的,因为你不会回到task.read() 行。 考虑使用线程库中的 Thread 模块,像这样导入它:

          from threading import Thread
          

          或按照其他人的建议使用按钮刷新值。

          【讨论】:

            猜你喜欢
            • 2021-05-24
            • 1970-01-01
            • 1970-01-01
            • 2015-04-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-28
            • 1970-01-01
            相关资源
            最近更新 更多