0

I have a python script that updates a variable every second, using the code:

import threading
m = 0
def printthis():
    threading.Timer(0.5,printthis).start()
    def f1():
        global m
        m = m + 1
        return
    f1()
printthis()

I would like the variable m to be updated on the idle shell every second. I dont want the program to just print 1, 2, 3, 4... I would like it to print 1, then refresh the shell to show what the variable has changed to.

finlx
  • 89
  • 9
  • This won't work in idle, it is not a real tty. Is idle a necessity? – Padraic Cunningham Sep 26 '15 at 17:25
  • [This SO question/answer](http://stackoverflow.com/a/5419488/1620879) should help. – jorgeh Sep 26 '15 at 18:13
  • 1. Idle is a development environment and generally not intended for routine running of already developed programs. An exception would be on Windows, where the unicode support is better than Command Prompt. 2. Python does not define the effect of control characters, including \r, when written to a 'file', including stdout and stderr. In universal newlines mode, Python itself interprets \r as \n. Putting 1 and 2 together, you should a program using \r directly with `python ` in a console window that interprets \r as 'return to beginning of line'. – Terry Jan Reedy Sep 27 '15 at 21:56

1 Answers1

0

If you want control of the output screen no matter how your program is run, create a screen you can control. The following tkinter program does what you requested, including when run with Idle, which is where quickly I developed it.

import tkinter as tk

root = tk.Tk()
text = tk.Text(root)
text.pack()

m = 0
def update():
    global m
    text.delete('1.0', 'end')
    text.insert('1.0', str(m))
    m += 1
    root.after(1000, update)
update()  # initialize cycle

root.mainloop()
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52