Bottom Page

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()
Quote
#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()
Quote
#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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Make Label Text background (default color) transparent using tkinter in python barry76 1 1,069 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 217 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  Find active PyQT5 textline and add result to dictionary mart79 3 302 Jul-31-2019, 03:25 PM
Last Post: Denni
  Unable to update or refresh label text in tkinter jenkins43 3 885 Jul-24-2019, 02:09 PM
Last Post: Friend
  TKinter GUI / Pandas Data frame Loop NSearch 0 1,257 Jun-26-2019, 12:37 AM
Last Post: NSearch
  [Tkinter] Line Number Tkinter Text Are smabubakkar 1 586 Jun-22-2019, 07:05 PM
Last Post: woooee
  how to insert image into Text widget Tkinter atlass218 5 2,250 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] How do I center this text in tkinter? ejected 4 8,345 Mar-29-2019, 10:35 PM
Last Post: woooee
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 1,270 Jan-23-2019, 08:45 AM
Last Post: Larz60+
  [Tkinter] tkinter - unexpected output - don't know how func is triggered before calling omm 8 767 Dec-11-2018, 08:09 PM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)