CLI+GUI

Michele Simionato mis6 at pitt.edu
Sat Aug 9 09:30:40 EDT 2003


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?
Thanks,

                Michele




More information about the Python-list mailing list