1

I have a GUI with label and entry widgets on a frame widget. I want to add a command prompt.to the frame through which the user can submit commands. I know about using the cmd module to create a prompt in the python shell, but I want the prompt to be part of my GUI window. Is there a way to 'embed' a command prompt in a canvas for example? Right now I am using Cmd.init(self) and cmdloop to start a prompt in python shell...

Just to clarify further, I would like the shell to run from within my GUI window (e.g. inside a canvas), and not as its own window. Is that possible?

  • Yes, I am. You know how to solve my problem? – MiddleClassMan Nov 13 '11 at 13:59
  • If you are using Linux, have a look here: http://stackoverflow.com/questions/7253448/how-to-embed-a-terminal. If you are using Windows, you will have to use a similar method, but with a CMD window instead of an xterm one. I don't believe CMD has an `-into` property, so you'll have to tell the OS to put the CMD window inside of the Tkinter one, as opposed to having its own window. This means that you will need to have a very good understanding of the Windows API. – D K Nov 13 '11 at 14:34

1 Answers1

0

The cmd module was made for terminals, where there's just one textual “widget” used for both input and output. This is not really suited for a GUI.

Here's an idea (inspired by web frameworks like Pyramid) that works better:

Have a read-only widget for the output, and a separate widget for the command entry.

When the user presses Enter, copy the command to the output box (prefixing with '>>>' and '...' as necessary), run the command (using compile_command and run_code from the code module, with redirecting sys.stdout to a StringIO that gets appended to the output), and then append the output in the output box.

Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81