Python Forum
Active tkinter text output during loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active tkinter text output during loop
#1
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

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()
Reply
#2
Sounds like a hogging the computer problem. A for or a while will generally do this.
Using Tkinter's after module instead will also solve the problem. Try using update_idletasks http://effbot.org/tkinterbook/widget.htm after the insert line. Also, you should be using a Listbox. if you just want to display the data. This works for me

    from tkinter import *
    import time

    def collectdata():
        ports = [1, 2, 3, 4, 5]
        ##length = len(ports)
        for p in ports:
            currentport.insert(END, p)
            window.update_idletasks()
            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 = Listbox(window)
    currentport.pack()

    window.mainloop()
Reply
#3
Thank you so much!

Something else I was wanting to do is have the current port being the only one that was displayed, instead of adding it at the end of a list of all the previous ports. I modified the for loop from what you suggested to do so:

for p in ports:
    currentport.delete(1.0, END) #this deletes everything in the box before printing out the next one
                                 #there may be a cleaner way to do this, but this works for me
    currentport.insert(END, n)
    window.update_idletasks()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,450 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  [Tkinter] Updating tkinter text BliepMonster 5 5,671 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,866 Apr-16-2022, 04:04 PM
Last Post: DBox
  Tkinter | entry output. Sap2ch 1 1,951 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  tkinter change the text of the checkbox zazas321 1 3,758 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 7,459 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,546 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 4,097 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984
Photo Tkinter TEXT background image _ShevaKadu 5 7,654 Nov-02-2020, 10:34 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020