Python 如何根据串行读取实时更改bg颜色?
这是我的第一个GUI 我想实时更改按钮的背景颜色。 也许是这样的烟雾,但我不知道该放在哪里(Python 如何根据串行读取实时更改bg颜色?,python,tkinter,pyserial,Python,Tkinter,Pyserial,这是我的第一个GUI 我想实时更改按钮的背景颜色。 也许是这样的烟雾,但我不知道该放在哪里( 可能是使用after函数,因为我现在的代码正在更改颜色,但有一个clik延迟,不是实时的。您可以使用按钮小部件中的命令调用函数来更改颜色 from tkinter import * root = Tk() def move_forward_pressed(): button1.configure(bg='red') def stop_pressed(): button1.conf
可能是使用after函数,因为我现在的代码正在更改颜色,但有一个clik延迟,不是实时的。您可以使用按钮小部件中的命令调用函数来更改颜色
from tkinter import *
root = Tk()
def move_forward_pressed():
button1.configure(bg='red')
def stop_pressed():
button1.configure(bg='green')
button1 = Button(root, text='move forward', bg='green', command=move_forward_pressed)
button1.pack()
button2 = Button(root, text='stop', command=stop_pressed)
button2.pack()
root.mainloop()
这是可行的,我在ser.read commmand之后添加了ser.flusInput和startogger,以便程序每隔100ms读取一次串行端口 现在按钮正在实时更改背景颜色(延迟100ms):
ser.read()
是如何工作的?它是阻塞的,还是会立即返回值?你必须说出b
是什么,b'1'
以及ser.read()
将要做什么
from tkinter import *
root = Tk()
def move_forward_pressed():
button1.configure(bg='red')
def stop_pressed():
button1.configure(bg='green')
button1 = Button(root, text='move forward', bg='green', command=move_forward_pressed)
button1.pack()
button2 = Button(root, text='stop', command=stop_pressed)
button2.pack()
root.mainloop()
def warning():
b = ser.read()
ser.flushInput()
print(b)
if b == b'1':
start_forButton.configure(bg='red')
if b == b'2':
start_backButton.configure(bg='red')
if b==b'0':
start_forButton.configure(bg='white')
start_backButton.configure(bg='white')
def startLogger():
while True:
warning()
sleep(0.1)