1
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()

the time.sleep instruction is only to be sure the window appear before all its done

Hello, Im trying to make a program, that show in a window the attempts to stablish a connection with a serial port. Im using TKinter in python the problem is that all the messages appear at the end of all the attempts happend but i want to show the message in each attempts.

Its my first time with Tkinter, im using python 3.11

  • 1
    Your GUI will not be updated till the while loop finishes. You should use something like a coroutine, tkinter offers `widget.after(ms, func, *args)` for that – Thingamabobs Mar 05 '23 at 13:46
  • ok. Where can I learn how to use it? I think I need the anser to How to substitute loops in Tkinter? – Santiago Vigo Mar 05 '23 at 14:19
  • 1
    An answer of mine that shows you how to use it, can be found [here](https://stackoverflow.com/a/63118515/13629335) or [here](https://stackoverflow.com/a/74218331/13629335) – Thingamabobs Mar 05 '23 at 15:22

0 Answers0