473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

CLI+GUI

I wonder what is the recommended way of using Tkinter
together with a command line oriented application.
I have in mind something like that:

import cmd,sys

class CMD(cmd.Cmd):
def do_this(self,*args):
draw_a_diagram()
def do_that(self,*args):
draw_another_diagram()
def do_exit(self,*args):
sys.exit(0)

c=CMD()
c.cmdloop()

Here the methods ``draw_a_diagram`` and ``draw_another_diagram``
should do what you expect. Notice that I don't want to use
different windows, the graphs should be displayed on the same
window one over the other. BTW, I am not really displaying
diagrams, this is only the simplest example I can figure out
of graphics program driven by a command line interpreter.
I want to stay with the CLI interface, which I like a lot
thanks to the readline and completion support, the easy
of use and the easy of implementation.

I have no idea of how to do that, except via contorsions
such as saving (a representation of) the diagrams in a file
and having a separated Tkinter process that periodically look at the
file, changing the display if the file has been updated. I
guess there is a better way, since this is a quite common issue.
Any suggestion?

TIA,

Michele
Jul 18 '05 #1
18 2771
mi**@pitt.edu (Michele Simionato) wrote in message news:<22**************************@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
Jul 18 '05 #2
Michele Simionato wrote:
mi**@pitt.edu (Michele Simionato) wrote in message news:<22**************************@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 <er*********@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #3
Eric Brunel <er*********@pragmadev.com> wrote in message news:<bh**********@news-reader3.wanadoo.fr>...
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


It hangs immediately when I start the script by clicking on its icon.
What do you mean with "I'd use an Event" ? I thought an Event object
is automatically generated when I click on a widget, or press a key,
or something. Are you saying can I can programmatically generate an
Event, faking a real mouse/key press? How so? That is something I
always wanted to know ;)
Michele
Jul 18 '05 #4
Michele Simionato wrote:
Eric Brunel <er*********@pragmadev.com> wrote in message
news:<bh**********@news-reader3.wanadoo.fr>...
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


It hangs immediately when I start the script by clicking on its icon.
What do you mean with "I'd use an Event" ? I thought an Event object
is automatically generated when I click on a widget, or press a key,
or something. Are you saying can I can programmatically generate an
Event, faking a real mouse/key press? How so? That is something I
always wanted to know ;)


When talking of event objects in a context of synchronizing threads,
it seems likely one is talking about threading.Event in specific. As
the online docs for the threading module say:

Event()
A factory function that returns a new event object. An event manages a flag
that can be set to true with the set() method and reset to false with the
clear() method. The wait() method blocks until the flag is true.

all details are at:

http://www.python.org/doc/current/li...t-objects.html
There is no implication whatsoever that such objects are "automatically
generated" by any GUI toolkit. The terminology confusions are, alas,
almost inevitable.
Alex

Jul 18 '05 #5
Michele Simionato wrote:
Eric Brunel <er*********@pragmadev.com> wrote in message news:<bh**********@news-reader3.wanadoo.fr>...
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

It hangs immediately when I start the script by clicking on its icon.
What do you mean with "I'd use an Event" ? I thought an Event object
is automatically generated when I click on a widget, or press a key,
or something.


I wasn't thinking about this Event class. I thought about the one described here:
http://www.python.org/doc/current/li...t-objects.html

It allows very basic communications between threads.
Are you saying can I can programmatically generate an
Event, faking a real mouse/key press? How so? That is something I
always wanted to know ;)


Generating a *Tk* event is quite easy: just use the event_generate method that
exists on all widgets. I used it to generate simple events, often user-defined
one. The syntax is simple:

myWidget.event_generate("<<MyEvent>>")

If you have a binding for "<<MyEvent>>" for myWidget, you can trigger it this way.

I never tried to pass event detail, though.

HTH
--
- Eric Brunel <er*********@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #6
| It hangs immediately when I start the script
| by clicking on its icon.

Michele ....

I experience hangs with SIMPLE Python/Tk scripts
regularly using Windows 98 and this has been
a source of much frustration, almost to the point
of giving up on Tk ....

Your tk_cli script works for me, displaying the Tk window,
accepting display whatEver commands, and displaying the
results back in the Tk window ....

However, it will hang _sometimes_ at the (Cmd) prompt
on the second pass through after displaying the first
time as expected ....

--
Cousin Stanley
Human Being
Phoenix, Arizona
Jul 18 '05 #7
Cousin Stanley wrote:
Michele ....

I experience hangs with SIMPLE Python/Tk scripts
regularly using Windows 98 and this has been
a source of much frustration, almost to the point
of giving up on Tk ....


Same problem, different solution: it made me give up on Windows 98... ;-)

FYI, and just to make this post a bit more useful, it seems to be a problem in
Windows 95/98, since no problem ever occured with the same scripts/distros on
Win2k or WinXP, not to mention Unices...
--
- Eric Brunel <er*********@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #8
Eric Brunel wrote:
Same problem, different solution: it made me give up on Windows 98... ;-)
quoting microsoft's lifecycle page:

"Mainstream support for Windows 98/98 SE ended on June 30th 2002,
and no-charge incident support and extended hotfix support ends on
June 30th 2003."
FYI, and just to make this post a bit more useful, it seems to be a problem in
Windows 95/98, since no problem ever occured with the same scripts/distros on
Win2k or WinXP, not to mention Unices...


iirc, the problem is that (some versions of the) Tk DLL locks up during the cleanup
phase (probably due to a race problem), and Win2k/XP is smart enough to kill the
process when that happens.

the sourceforge bug tracker has more details.

</F>


Jul 18 '05 #9
| Same problem, different solution:
| it made me give up on Windows 98 ...

Eric ...

I do plan on eventually moving to a Linux distribution
in the near future, but have to deal with what I have
at the moment ...

The limited experimentation that I've done using Knoppix,
proved to me that Tk under Win98 was the problem
as the same scripts that hang intermittently under Win98
ran under Knoppix without any problems ...

It's also good to hear that the Tk problems
under later versions of Windows are alleviated ...

Thanks for the info ...

--
Cousin Stanley
Human Being
Phoenix, Arizona
Jul 18 '05 #10
Eric Brunel <er*********@pragmadev.com> wrote in message news:<bh**********@news-reader3.wanadoo.fr>...
Cousin Stanley wrote:
Michele ....

I experience hangs with SIMPLE Python/Tk scripts
regularly using Windows 98 and this has been
a source of much frustration, almost to the point
of giving up on Tk ....


Same problem, different solution: it made me give up on Windows 98... ;-)

FYI, and just to make this post a bit more useful, it seems to be a problem in
Windows 95/98, since no problem ever occured with the same scripts/distros on
Win2k or WinXP, not to mention Unices...


These are very good news to me, since it means that the fault is not
mine
and that the approach I came out is not unreasonable (I hope). No bad,
for my first program using thread ;) For the record, I NEVER use
Windows for development(actually I use it only for watching DVDs),
nevertheless I
wanted to try the script to check about portability issues.
Michele
Jul 18 '05 #11
Hello Michele,
I wonder what is the recommended way of using Tkinter
together with a command line oriented application.

I know it's not Tkinter but what about wxPython with wxCrust?

HTH.
Miki
Jul 18 '05 #12
dan
Late to this thread, but --

in a similar situation, I just put:

_tkinter.dooneevent(_tkinter.DONT_WAIT)

in my main logic loop (cmd interpreter in your case), instead of
calling Frame.mainloop(). I believe Frame.update() does something
similar but for some reason this worked better.

ISTM this would be cleaner (and safer) than using threads. You can do
all your draw operations from your command line routines, and they
will get displayed as soon as the routine returns to your main loop to
wait for more input.

Am I missing something?

-dbm

mi**@pitt.edu (Michele Simionato) wrote in message news:<22**************************@posting.google. com>...
I wonder what is the recommended way of using Tkinter
together with a command line oriented application.
I have in mind something like that:

import cmd,sys

class CMD(cmd.Cmd):
def do_this(self,*args):
draw_a_diagram()
def do_that(self,*args):
draw_another_diagram()
def do_exit(self,*args):
sys.exit(0)

c=CMD()
c.cmdloop()

Here the methods ``draw_a_diagram`` and ``draw_another_diagram``
should do what you expect. Notice that I don't want to use
different windows, the graphs should be displayed on the same
window one over the other. BTW, I am not really displaying
diagrams, this is only the simplest example I can figure out
of graphics program driven by a command line interpreter.
I want to stay with the CLI interface, which I like a lot
thanks to the readline and completion support, the easy
of use and the easy of implementation.

I have no idea of how to do that, except via contorsions
such as saving (a representation of) the diagrams in a file
and having a separated Tkinter process that periodically look at the
file, changing the display if the file has been updated. I
guess there is a better way, since this is a quite common issue.
Any suggestion?

TIA,

Michele

Jul 18 '05 #13
da*******@yahoo.com (dan) wrote in message news:<fb*************************@posting.google.c om>...
Late to this thread, but --

in a similar situation, I just put:

_tkinter.dooneevent(_tkinter.DONT_WAIT)

in my main logic loop (cmd interpreter in your case), instead of
calling Frame.mainloop(). I believe Frame.update() does something
similar but for some reason this worked better.

ISTM this would be cleaner (and safer) than using threads. You can do
all your draw operations from your command line routines, and they
will get displayed as soon as the routine returns to your main loop to
wait for more input.

Am I missing something?

-dbm


I like quite a lot you suggestion! "dooneevent" was the method I was
looking for! Actually, I would rather prefer to avoid threads for such a
simple program. Thanks to the power of Python I wrote down a working
script in less than five minutes, even if probably I will need more
than five minutes to understand what I wrote ;)
Here it is:

import Tkinter as t
import cmd

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)
root.tk.dooneevent(0)
def do_quit(self,arg):
root.quit()
return 'quit' # anything != None will do

Cmd().cmdloop()
I will later try it on Windows 98. Dunno exactly what "dooneevent" is doing,
I searched my python directories for "dooneevent" and found only one
usage of "doonevent" and copied it ;) Unfortunately "dooneevent"
has no docstring, however few experiments show that "dooneevent()"
is the same that "dooneevent(0)" whereas "dooneevent(1)" hangs up
(it is waiting for what??)

Thanks for your help,
Michele
Jul 18 '05 #14
dan
da*******@yahoo.com (smarter_than_you) wrote in message
Also note that there are at least three ways to get this behavior:

_tkinter.dooneevent(TCL_DONT_WAIT)
Frame.update()
Tkinter.dooneevent(0) #this is new to me! You found a third way to
call it

that would be:

root.tk.dooneevent()

but anyway --

in my experiments, this last way (Michelle's way) has very bad timing,
if you care about that sort of thing. Both the _tkinter call and
update() seem to be more reactive.
Jul 18 '05 #15
da*******@yahoo.com (dan) wrote in message news:<fb*************************@posting.google.c om>...
da*******@yahoo.com (smarter_than_you) wrote in message
Also note that there are at least three ways to get this behavior:

_tkinter.dooneevent(TCL_DONT_WAIT)
Frame.update()
Tkinter.dooneevent(0) #this is new to me! You found a third way to
call it

that would be:

root.tk.dooneevent()

but anyway --

in my experiments, this last way (Michelle's way) has very bad timing,
if you care about that sort of thing. Both the _tkinter call and
update() seem to be more reactive.


..update() seems to be working (even of Win98) and the name is
self-documenting, so at the moment it is my first candidate.

BTW, notice the spelling : Michele with one "l", italian male name,
as opposed to Michelle, with two "l", french female name.

Thanks, for your help,

Michele
Jul 18 '05 #16
mi**@pitt.edu (Michele Simionato) writes:
I wonder what is the recommended way of using Tkinter
together with a command line oriented application.
I have in mind something like that:

[...]

Um. I'm coming to this thread late, but my pyrepl package works quite
nicely with Tkinter programs (on Unix, anyway).

$ pythoni
Python 2.2.1 (#1, Apr 9 2002, 13:10:27)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
->> import Tk
Traceback (most recent call last):
File "<input>", line 2, in ?
ImportError: No module named Tk
->> import Tkinter
->> root = Tkinter.Tk()
->> label = Tkinter.Label(root, text="hi!")
->> label.pack()

.... just like the native toplevel (which isn't surprising, as that's
where I cribbed the code from).

HTH,
mwh

--
QNX... the OS that walks like a duck, quacks like a duck, but is,
in fact, a platypus. ... the adventures of porting duck software
to the platypus were avoidable this time.
-- Chris Klein, alt.sysadmin.recovery
Jul 18 '05 #17
mi**@pitt.edu (Michele Simionato) writes:
I will later try it on Windows 98. Dunno exactly what "dooneevent"
is doing,


"man Tk_DoOneEvent"

Cheers,
mwh

--
I appear to have caused some confusion. Excellent.
-- JoeB, asr
Jul 18 '05 #18
Michael Hudson <mw*@python.net> writes:
mi**@pitt.edu (Michele Simionato) writes:
I will later try it on Windows 98. Dunno exactly what "dooneevent"
is doing,


"man Tk_DoOneEvent"


shit, that should be Tcl_DoOneEvent...

Cheers,
mwh

--
One of the great skills in using any language is knowing what not
to use, what not to say. ... There's that simplicity thing again.
-- Ron Jeffries
Jul 18 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: gamaron | last post by:
Summary --------- Is Fox Toolkit ( http://www.fox-toolkit.org/ ) the "best" C++ GUI library/toolkit? My key goals (at least those that come to mind right now): * Portability (Windows, Linux,...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
5
by: Alfonso Morra | last post by:
Hi, I am a C/C++ developer of quite a few yesrs, although I am relatively new to Windows (Unix background). I am about to begin work on a project that would require me to develop several GUI...
4
by: Ian | last post by:
I would like to hear from others who have considered and/or ported code from traditional C++ to C++/CLI. The class library I am considering porting to C++/CLI was written in traditional C++ with...
12
by: Philip | last post by:
Does anyone know any commercial C++/CLI GUI library with source code? Thank you very much.
2
by: John Doe | last post by:
Hi, Tried searching Google on this topic but not many came up. Basically I have a C# GUI client that wants to receive 'notifications' from a worker DLL (which is implemented in C++/CLI). So far,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.