多线程中的按钮问题 Python Tkinter Serial Monitor
Button problem in multi-threading Python Tkinter Serial Monitor
我尝试创建一个串行监视器,它可以使用 Tkinter 作为 GUI 从串行端口发送和接收消息。当程序搜索新数据时,我做了两个线程,因为没有冻结的 GUI。
Tkinter GUI 很简单:
- 1 个用于放置要发送的文本的条目
- 1 个标签用于显示收到的消息
- 1 个用于发送条目消息的按钮
我的问题是当我 运行 程序点击按钮时,没有任何反应。我认为当我启动我的两个线程时按钮不再加载。我该如何解决这个问题?
import threading
from tkinter import *
import tkinter as tk
import serial
root = tk.Tk()
COM = "COM3"
ser = serial.Serial(port=COM, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)
ser.isOpen()
msg = ""
# read Serial
def ReadSerial():
global msg
msg = ""
msg = ser.readline()[:-2].decode("utf-8")
if msg != "":
print(msg)
return msg
# write Serial
def WriteSerial(sendmsg):
print("send")
ser.write(bytes(sendmsg, 'utf-8'))
ReadSerial()
# Tkinter
root.title("WIP NAME")
root.geometry("650x400")
inputData = Entry(root, text="<Slave1&p>") # input for enter the message to write
entrymsg = inputData.get() # get the massage
buttonMsg = Button(root, text="send", command = WriteSerial(entrymsg)) # create a send button for send the message
readData = Label(root, text=msg) # show message in Tkinter
ReadSerial()
# show items
inputData.grid()
readData.grid()
buttonMsg.grid()
# GUI thread
def TkinterGui():
while 1==1:
global msg
entrymsg = inputData.get()
# Serial thread
def SerialProgram():
while 1==1:
ReadSerial()
readData.update_idletasks()
x = threading.Thread(target=TkinterGui, args=())
y = threading.Thread(target=SerialProgram, args=())
x.start()
y.start()
root.mainloop()
非常感谢@acw1668,我已经在这个项目上工作了一个星期。
正确的命令:
buttonMsg = Button(root, text="send", command = lambda: WriteSerial(inputData.get()))
和整个代码:
import threading
from tkinter import *
import tkinter as tk
import serial
root = tk.Tk()
COM = "COM3"
ser = serial.Serial(port=COM, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)
ser.isOpen()
msg = ""
# read Serial
def ReadSerial():
global msg
msg = ""
msg = ser.readline()[:-2].decode("utf-8")
if msg != "":
print(msg)
return msg
# write Serial
def WriteSerial(sendmsg):
print("send")
ser.write(bytes(sendmsg, 'utf-8'))
ReadSerial()
# Tkinter
root.title("WIP NAME")
root.geometry("650x400")
inputData = Entry(root, text="<Slave1&p>") # input for enter the message to write
entrymsg = inputData.get() # get the massage
buttonMsg = Button(root, text="send", command = lambda: WriteSerial(inputData.get())) # create a send button for send the message
readData = Label(root, text=msg) # show message in Tkinter
ReadSerial()
# show items
inputData.grid()
readData.grid()
buttonMsg.grid()
# GUI thread
def TkinterGui():
while 1==1:
global msg
entrymsg = inputData.get()
# Serial thread
def SerialProgram():
while 1==1:
ReadSerial()
readData.update_idletasks()
x = threading.Thread(target=TkinterGui, args=())
y = threading.Thread(target=SerialProgram, args=())
x.start()
y.start()
root.mainloop()
P.S.: 对不起,如果我的英语不太好理解,我是法国人:)
我尝试创建一个串行监视器,它可以使用 Tkinter 作为 GUI 从串行端口发送和接收消息。当程序搜索新数据时,我做了两个线程,因为没有冻结的 GUI。
Tkinter GUI 很简单:
- 1 个用于放置要发送的文本的条目
- 1 个标签用于显示收到的消息
- 1 个用于发送条目消息的按钮
我的问题是当我 运行 程序点击按钮时,没有任何反应。我认为当我启动我的两个线程时按钮不再加载。我该如何解决这个问题?
import threading
from tkinter import *
import tkinter as tk
import serial
root = tk.Tk()
COM = "COM3"
ser = serial.Serial(port=COM, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)
ser.isOpen()
msg = ""
# read Serial
def ReadSerial():
global msg
msg = ""
msg = ser.readline()[:-2].decode("utf-8")
if msg != "":
print(msg)
return msg
# write Serial
def WriteSerial(sendmsg):
print("send")
ser.write(bytes(sendmsg, 'utf-8'))
ReadSerial()
# Tkinter
root.title("WIP NAME")
root.geometry("650x400")
inputData = Entry(root, text="<Slave1&p>") # input for enter the message to write
entrymsg = inputData.get() # get the massage
buttonMsg = Button(root, text="send", command = WriteSerial(entrymsg)) # create a send button for send the message
readData = Label(root, text=msg) # show message in Tkinter
ReadSerial()
# show items
inputData.grid()
readData.grid()
buttonMsg.grid()
# GUI thread
def TkinterGui():
while 1==1:
global msg
entrymsg = inputData.get()
# Serial thread
def SerialProgram():
while 1==1:
ReadSerial()
readData.update_idletasks()
x = threading.Thread(target=TkinterGui, args=())
y = threading.Thread(target=SerialProgram, args=())
x.start()
y.start()
root.mainloop()
非常感谢@acw1668,我已经在这个项目上工作了一个星期。 正确的命令:
buttonMsg = Button(root, text="send", command = lambda: WriteSerial(inputData.get()))
和整个代码:
import threading
from tkinter import *
import tkinter as tk
import serial
root = tk.Tk()
COM = "COM3"
ser = serial.Serial(port=COM, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)
ser.isOpen()
msg = ""
# read Serial
def ReadSerial():
global msg
msg = ""
msg = ser.readline()[:-2].decode("utf-8")
if msg != "":
print(msg)
return msg
# write Serial
def WriteSerial(sendmsg):
print("send")
ser.write(bytes(sendmsg, 'utf-8'))
ReadSerial()
# Tkinter
root.title("WIP NAME")
root.geometry("650x400")
inputData = Entry(root, text="<Slave1&p>") # input for enter the message to write
entrymsg = inputData.get() # get the massage
buttonMsg = Button(root, text="send", command = lambda: WriteSerial(inputData.get())) # create a send button for send the message
readData = Label(root, text=msg) # show message in Tkinter
ReadSerial()
# show items
inputData.grid()
readData.grid()
buttonMsg.grid()
# GUI thread
def TkinterGui():
while 1==1:
global msg
entrymsg = inputData.get()
# Serial thread
def SerialProgram():
while 1==1:
ReadSerial()
readData.update_idletasks()
x = threading.Thread(target=TkinterGui, args=())
y = threading.Thread(target=SerialProgram, args=())
x.start()
y.start()
root.mainloop()
P.S.: 对不起,如果我的英语不太好理解,我是法国人:)