Command config, quitting, binary, Timer

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Sep 3 21:44:11 EDT 2005


Hello, I have four things to ask or to suggest, sorry if they seem
basic or already discussed.

-------------------

I am still ignorant about Tkinter. This little program, after pressing
the "Go" eats more and more RAM, is it normal? Can it be avoided? (In
normal programs this is isn't a real problem).

! import Tkinter
! def dogo():
!     while 1:
!         b.config(command=lambda:None)
! root = Tkinter.Tk()
! b = Tkinter.Button(root, text="Go", command=dogo)
! b.pack()
! root.mainloop()

Note: I have found this problem because in a small program I have a
Start button that becomes a Stop, so the bound command must change each
time. (Maybe there is some other solution, like hiding/unhiding the
buttons, I don't know.)

-------------------

When I quit a Tkinter program (clicking the [X] button of the window)
while it's running, it stops with something like this:

TclError: invalid command name ".9970192"

There is a way to avoid it, or to intercept that quitting command to
stop the program more gracefully?

-------------------

When I have to convert numbers to binary I use something like this (the
management of negative numbers is removed). It's my faster Python
version (derived by a Hettinger's version):

! from collections import deque
! def binary_conv(n):
!     if n == 0:
!         return 0
!     else:
!         absn = abs(n)
!         conv = ["0", "1"]
!         result = deque()
!         _app = result.appendleft
!         while absn:
!             _app( conv[absn & 1] )
!             absn >>= 1
!         return int("".join(result))

But converting to binary is a quite common operation, so I think it can
be added as a binary function to Python, for example adding "b" to the
the conversion types of the % formatting.

-------------------

Sometimes I need something like the Timer of Java, it generates "ticks"
and calls a function for each tick. The tick frequency can be set, or
they can be stopped.

In another Newsgroup someone has suggested me that the callLater of
Twisted can solve my problem, but I think it's a quite common thing, so
I think that maybe it can be added to the threading standard module.

This is a rough Python version of mine (without comments), it's not a
true Metronome because it counts the delay time after the end of the
last function call. This class also seems fragile, sometimes it gives
me problems, and I cannot use too much concurrent metronomes, etc. It's
quite

Maybe someone can suggest me how to improve it.


! from threading import Timer
!
! class Metronome(object):
!     def __init__(self, interval, fun, *args, **kwargs):
!         self.interval = interval # seconds.
!         self.fun = fun
!         self.args = args
!         self.kwargs = kwargs
!         self._running = False
!     def _go(self):
!         # Call the function with the stored values
!         self.fun(*self.args, **self.kwargs)
!         if self._running:
!             self._call = Timer(self.interval, self._go)
!             self._call.start()
!     def start(self):
!         if not self._running:
!             self._running = True
!             self._go()
!     def stop(self):
!         if self._running:
!             self._call.cancel()
!             self._running = False

Thank you,
bearophile




More information about the Python-list mailing list