from tkinter import Tk,Text,END
import serial
import time
class P:
def __init__(self, window):
self.window=window
self.window.title("Multiple lines")
#Adding text box
self.screen=Text(self.window, state="disabled", width=40, height=15, background="white", foreground="black", font=("Helvetica",10))
#Putting the screen in the window
self.screen.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
def conect(self, port):
conectionDone=False
try:
arduino = serial.Serial(port, 9600)
conectionDone=True
except:
self.showInScreen("Not Arduino in port: ")
self.showInScreen(port)
self.showInScreen("\n")
return conectionDone
def showInScreen(self, valor):
self.screen.configure(state="normal")
self.screen.insert(END, valor)
self.screen.configure(state="disabled")
return
if __name__ == "__main__":
window_main=Tk()
conection=P(window_main)
ports=['COM1','COM2','COM3','COM4','COM5','COM6','COM7','COM8','COM9','COM10','COM11','COM12','COM13','COM14']
portDefined=False
index=-1
while not(portDefined):
index+=1
if index<14:
portDefined=conection.conect(ports[index])
time.sleep(0.2)
else:
conection.showInScreen("Conection Failed")
break
if portDefined:
conection.showInScreen("Conection Established in port: ")
conection.showInScreen(ports[index])
window_main.mainloop()
time.sleep 指令只是为了确保窗口在所有完成之前出现
你好,我正在尝试制作一个程序,在窗口中显示尝试与串行端口建立连接的尝试。我在 python 中使用 TKinter,问题是所有消息都出现在所有尝试结束时,但我想在每次尝试中显示消息。
这是我第一次使用 Tkinter,我使用的是 python 3.11