-1

I have a BASH script running that opens a program (tshark) which writes a bunch of values to a logfile. The script then counts the unique values and writes the (count of the) uniques values from the last 3 minutes to a logfile (count3m.log) It also opens a python script. The python is there to show a window with the values from count3m.log. As the value in count3m.log changes every 30 seconds I want to keep looking for a new value from count3m. I tried it with the code below. It only executes the loop once. What am I doing wrong?

#!/usr/bin/env python

import sys
import re
import time
from Tkinter import *

while True:  

    root = Tk()
    count3m = open('count3m.log','r')
    countStart = open('countStart.log','r')


    minutes = Label(root, text="Uniq signals < 3m ago:")
    minutes.grid(row=0, column=0)

    minutes = Label(root, text=count3m.read())
    minutes.grid(row=1, column=0)
    count3m.close

    minutes = Label(root, text="Uniq signals since start:")
    minutes.grid(row=0, column=1)

    minutes = Label(root, text=countStart.read())
    minutes.grid(row=1, column=1)
    countStart.close
    time.sleep(5)
    print "test"
    root.mainloop()
2
  • 3
    When your code gets to root.mainloop() it "essentially" stops. root.mainloop() instantiates Tkinter and at this point your program is entirely waiting for an event to happen, not continuing to run code. Commented Apr 24, 2015 at 13:47
  • What is root.mainloop()? I think it might be hanging your loop (an endless loop inside an endless loop). Commented Apr 24, 2015 at 13:48

1 Answer 1

2

Referencing this answer

mainloop is really nothing more than an infinite loop that looks roughly like this (those aren't the actual names of the methods, the names merely serve to illustrate the point):

while True:
    event=wait_for_event()
    event.process()
    if main_window_has_been_destroyed(): 
        break

So, you have an infinite loop inside your loop.

In order to update your label, you'll need to attach an event to your root. Also, set your label's textvariable = a StringVar. Then, update the StringVar in the event, and it will change the label.

Something like this

text  = StringVar()
label = Label(root, textvariable=text)
label.pack()

def update_label():
  text.set("new stuff")
  #update again
  root.after(SOME_TIME, update_label)

#the first update
root.after(SOME_TIME, update_label)
root.mainloop()

That should give you the basic idea. Relate stack overflow questions:

Making python/tkinter label widget update?

Python: Is it possible to create an tkinter label which has a dynamic string when a function is running in background?

Sign up to request clarification or add additional context in comments.

1 Comment

It took some looking at the other topics but finally got it to work. Thank you for helping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.