CLI+GUI
Bengt Richter
bokr at oz.net
Sat Aug 9 12:09:29 EDT 2003
On 9 Aug 2003 06:30:40 -0700, mis6 at pitt.edu (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!)
>
>BTW, I have another question, why the Cmd class does not have a default quit
>method? Looking at the source I see that any method returning something
>different from None will stop the command loop, and so I have used this
>hack, but I don't like it. Maybe I have missed something in the documentation?
Thank you for exploring this. I have wanted light weight canvas to paint on from
a CLI program, but tk would be nice too. I'm trying to prototype my UFF file idea
right now, but this is also interesting.
The above looks a little iffy to me off hand, unless there is some built in thread
safety magic in cmd loop doing s.set(arg). I.e., isn't s bound in the main thread
and the s.set(arg) call in the other thread?
I think I'd look at the queue module, or maybe socket.
Probably you are already there ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list