Dear friends,
is there a way to execute a dos command in a frame inside a wx.window GUI (and not in the usual external window)?
Thanks

Dani AI

Generated

Short answer: yes — you can show a DOS command's output inside a wx window. As pointed out, wx is a GUI toolkit and does not itself run shell commands; you must start the external process (as and suggested) and pipe its output into a widget. The practical additions you need are: run the process off the main thread, stream its stdout/stderr line-by-line, and update the wx widgets from the GUI thread (use wx.CallAfter).

A minimal, practical pattern (conceptual) is:

import subprocess, threading, wx

def run_command(textctrl, cmd):
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                         bufsize=1, universal_newlines=True, shell=False)
    for line in iter(p.stdout.readline, ''):
        wx.CallAfter(textctrl.AppendText, line)
    p.stdout.close()
    p.wait()
    wx.CallAfter(textctrl.AppendText, '\n[Process finished]\n')

# Start the worker thread:
# threading.Thread(target=run_command, args=(myTextCtrl, ['ping','-n','4','example.com']), daemon=True).start()

Notes and troubleshooting:

  • Use wx.CallAfter (or wx.PostEvent) to avoid cross-thread GUI calls.
  • Prefer shell=False and pass a list of args for safety; use ['cmd','/c','dir', ...] or shell=True only when you need shell builtins.
  • Combine stderr to stdout (shown above) so error lines appear in the same widget.
  • Interactive programs that expect a real TTY may not behave correctly when connected to pipes; those require a PTY/console-emulator.
  • Large output can deadlock if not read continuously; streaming per-line in a thread avoids that.

This approach gives the same user experience as the "details" pane in package managers: live, scrollable output embedded in your wx application.

Recommended Answers

All 5 Replies

No, I think. You must execute the command by yourself. wx is only for window management.

I see, so just to clarify, there is no way to "capture" the dos output in a window, isn't it?
What I would like to do is similar to what happens in linux when you want to install one package with synaptic and you click to show the details during the installation (sorry but I don't know how else to explain what I need to do).
If this is not possible, thanks anyway for your help,
G.

Take a look at:
http://www.daniweb.com/software-development/python/code/445504/an-almost-ide-wxpython

Should have enough hints how to do this with wxPython.

Basically ...

# using module subprocess to get the
# directory of drive C:\
# Python27 code

import subprocess

p = subprocess.Popen("dir C:\\", shell=True, stdout=subprocess.PIPE)

# allow external program to work
p.wait()

# read the result to a string
# (Python3 gives bytearray rather than string)
result = p.stdout.read()

print(result)

The wxWin tool set is a GUI library, but you can run any C or C++ code inside your wx functions. There are C functions which will run an external program and have the ability to pipe (as shown in the python example posted by vegaseat) the output to the calling process. You capture that output and stick it in an appropriate GUI widget.

Note that wxPython is the Python wrapper for the C/C++ based wxWindows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.