I'm writing a script to speak to a pressure scanning unit. The scanner has 32 ports, and the code asks the user which ports to use, then takes that input in as a list, creates the address for each port number, send an address to the scanner, reads the signal, and then moves on to the next address. I'm using the tkinter library to build a GUI, and it uses an entry field for the user input (port list). I'd like to have the GUI print out the current port the module is reading from each time it gets updated, so I have the code below to do that. However, it doesn't update live every time. Instead, it waits until the entire loop is finished (i.e. the script is done), then prints out the list of all the ports to the GUI output window.
Here is some code building the GUI
I decided it would be nice to include a block of code that you can actually do something with. I don't want to supply the original script because a) it's long, and b) it's written for a Raspberry Pi. So anyway, this code below is basically the same as what I'm using in my other script. Run it, it opens a window, you click the button, and it goes through a loop. I want the output to update live as it cycles the loop, but instead it just waits until the loop is finished, then prints the whole list out to the output box. The time.sleep line is to emulate the original program sitting on a port while the module scans it and calculates the average output signal over that time period.
Hopefully this will allow you to quickly edit and suggest changes I can make to get the output I'm looking for.
Here is some code building the GUI
window = Tk() window.title("Pressure Scanner") window.geometry('300x300') Label(window, text = "Ports used, separated by spaces:").pack() portlist = Entry(window) portlist.pack() Label(window, text = "Time to scan each port, in seconds:").pack() timeduration = Entry(window) timeduration.pack() ##other stuff - includes a button to run the main data collection function #make the current port output box on the GUI window Label(window, text = "Current Port").pack() currentport = Text(window) currentport.pack() window.mainloop()here is the section in the function that does the stuff in question. It loops through the port list to create the address and read the signal. When I say "currentport.insert(...)" I want this to update on the GUI window, not wait until all the ports have been scanned, then populate the field with which ports were scanned
# earlier in the function I take the input text from the "portlist" entry window on the GUI and create a list with the port numbers for j in range(length): currentport.insert(END, ports[j]+1) #ports[j]+1 because the addressing starts from 0 where the engraved port numbers on the module start from 1 #create address from port number #send address to module #receive signal, do math #go back to top of loop to read the next port
I decided it would be nice to include a block of code that you can actually do something with. I don't want to supply the original script because a) it's long, and b) it's written for a Raspberry Pi. So anyway, this code below is basically the same as what I'm using in my other script. Run it, it opens a window, you click the button, and it goes through a loop. I want the output to update live as it cycles the loop, but instead it just waits until the loop is finished, then prints the whole list out to the output box. The time.sleep line is to emulate the original program sitting on a port while the module scans it and calculates the average output signal over that time period.
Hopefully this will allow you to quickly edit and suggest changes I can make to get the output I'm looking for.
from tkinter import * import time def collectdata(): ports = [1, 2, 3, 4, 5] length = len(ports) for n in range(length): currentport.insert(END, ports[n]) time.sleep(2) window = Tk() window.title("Pressure Scanner") window.geometry('300x300') button = Button(window, text = "Collect Data", command = collectdata).pack() Label(window, text = "Output below should update every loop cycle") Label(window, text = "Current Port").pack() currentport = Text(window) currentport.pack() window.mainloop()