Python 运行 GUI 而串行 reader 是 运行ning
Python run GUI while serial reader is running
我必须构建一个维护 GUI 的应用程序,同时连续 reader 在后台不断 运行。串行 reader 更新我需要在我的 GUI 上显示的变量。到目前为止我有这个:
# These variables are updated by the reader.
var1 = 0
var2 = 0
var3 = 0
#Serial reader
def readserial(self):
ser = serial.Serial(port='COM4', baudrate=9600, timeout=1)
while 1:
b = ser.readline()
if b.strip():
#Function to set variables var1,var2,var3
handle_input(b.decode('utf-8'))
#Simple GUI to show the variables updating live
root = Tk()
root.title("A simple GUI")
gui_var1 = IntVar()
gui_var1.set(var1)
gui_var2 = IntVar()
gui_var2.set(var2)
gui_var3 = IntVar()
gui_var3.set(var3)
root.label = Label(root, text="My Gui")
root.label.pack()
root.label1 = Label(root, textvariable=gui_var1)
root.label1.pack()
root.label2 = Label(root, textvariable=gui_var2)
root.label2.pack()
root.label3 = Label(root, textvariable=gui_var3)
root.label3.pack()
root.close_button = Button(root, text="Close", command=root.quit)
root.close_button.pack()
#Start GUI and Serial
root.mainloop()
readserial()
就像现在一样,我的图形用户界面打开了,一旦我关闭它,序列号就会开始读取。
您可以定期使用 root.after(miliseconds, function_name_without_brackets)
到 运行 功能 readserial
- 无需 while 1
.
使用虚拟 COM 端口 /dev/pts/5
、/dev/pts/6
、
在 Linux 上测试
import tkinter as tk
import serial
# --- functions ---
def readserial():
b = ser.readline()
if b.strip():
label['text'] = b.decode('utf-8').strip()
# run again after 100ms (mainloop will do it)
root.after(100, readserial)
# --- main ---
ser = serial.Serial(port='COM4', baudrate=9600, timeout=1)
#ser = serial.Serial(port='/dev/pts/6', baudrate=9600, timeout=1)
root = tk.Tk()
label = tk.Label(root)
label.pack()
button = tk.Button(root, text="Close", command=root.destroy)
button.pack()
# run readserial first time after 100ms (mainloop will do it)
root.after(100, readserial)
# start GUI
root.mainloop()
我必须构建一个维护 GUI 的应用程序,同时连续 reader 在后台不断 运行。串行 reader 更新我需要在我的 GUI 上显示的变量。到目前为止我有这个:
# These variables are updated by the reader.
var1 = 0
var2 = 0
var3 = 0
#Serial reader
def readserial(self):
ser = serial.Serial(port='COM4', baudrate=9600, timeout=1)
while 1:
b = ser.readline()
if b.strip():
#Function to set variables var1,var2,var3
handle_input(b.decode('utf-8'))
#Simple GUI to show the variables updating live
root = Tk()
root.title("A simple GUI")
gui_var1 = IntVar()
gui_var1.set(var1)
gui_var2 = IntVar()
gui_var2.set(var2)
gui_var3 = IntVar()
gui_var3.set(var3)
root.label = Label(root, text="My Gui")
root.label.pack()
root.label1 = Label(root, textvariable=gui_var1)
root.label1.pack()
root.label2 = Label(root, textvariable=gui_var2)
root.label2.pack()
root.label3 = Label(root, textvariable=gui_var3)
root.label3.pack()
root.close_button = Button(root, text="Close", command=root.quit)
root.close_button.pack()
#Start GUI and Serial
root.mainloop()
readserial()
就像现在一样,我的图形用户界面打开了,一旦我关闭它,序列号就会开始读取。
您可以定期使用 root.after(miliseconds, function_name_without_brackets)
到 运行 功能 readserial
- 无需 while 1
.
使用虚拟 COM 端口 /dev/pts/5
、/dev/pts/6
、
import tkinter as tk
import serial
# --- functions ---
def readserial():
b = ser.readline()
if b.strip():
label['text'] = b.decode('utf-8').strip()
# run again after 100ms (mainloop will do it)
root.after(100, readserial)
# --- main ---
ser = serial.Serial(port='COM4', baudrate=9600, timeout=1)
#ser = serial.Serial(port='/dev/pts/6', baudrate=9600, timeout=1)
root = tk.Tk()
label = tk.Label(root)
label.pack()
button = tk.Button(root, text="Close", command=root.destroy)
button.pack()
# run readserial first time after 100ms (mainloop will do it)
root.after(100, readserial)
# start GUI
root.mainloop()