CLI+GUI
Eric Brunel
eric.brunel at pragmadev.com
Mon Aug 11 03:58:02 EDT 2003
Michele Simionato wrote:
> mis6 at pitt.edu (Michele Simionato) wrote in message news:<2259b0e2.0308080948.5a95e383 at posting.google.com>...
>
>>I wonder what is the recommended way of using Tkinter
>>together with a command line oriented application.
>
>
> Replying to myself ...
>
> I tried to implement what I discussed in my previous mail via the
> threading module:
>
> #cmdriven.py
>
> import Tkinter as t
> import cmd,threading
>
> root=t.Tk()
> s=t.StringVar()
> s.set('ciao')
> label=t.Label(root,textvariable=s)
> label.pack()
>
> class Cmd(cmd.Cmd):
> def do_display(self,arg):
> s.set(arg)
> def do_quit(self,arg):
> root.quit()
> return 'quit' # anything <> None will do the job
>
> def cmdloop(stringvar):
> try: Cmd().cmdloop()
> finally: pass # gracefully exit if sometimes goes wrong
>
> thread=threading.Thread(target=cmdloop,args=(s,))
> thread.start()
> root.mainloop()
>
> It works if I do something like
>
> $ python cmdriven.py
> (Cmd) display hello
> (Cmd) display It works!
> (Cmd) quit
>
> However, I wonder if this is a robust solution and if I should expect
> problems in more complicate situations (some time passes ... I have
> just discovered that this script hangs under Windows 98!)
I don't know if this is the problem, because you didn't say exactly when the
script hangs, but Tkinter apparently has problems when calls to it are made from
a thread different from the one into which it was initialized. I'd use an Event
between Tkinter's thread and Cmd's thread, checking it regularly with Tkinter's
after method.
HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list