2

How to run command prompt inside tkinter frame? I know how to do it with "pygame", but those methods doesn't work - command prompt just runs in another window.

I tried to do it like this:

import tkinter as tk
import os
import subprocess

root = tk.Tk()
root.geometry("640x480")
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)

child_env = dict(os.environ)
child_env["SDL_WINDOWID"] = str(frame.winfo_id())
child_env["SDL_VIDEODRIVER"] = "windib"
p = subprocess.Popen(["cmd.exe"], env=child_env)

root.mainloop()

But, as i said, it doesn't work.

4
  • This has been asked several times. Commented Aug 20, 2018 at 12:59
  • Possible duplicate of Redirect command line results to a tkinter GUI Commented Aug 20, 2018 at 13:01
  • Currently the only way to do this that supports all Windows console applications is to hide the console window and use the low-level console API to constantly poll the input buffer and active screen buffer for changes. This is how alternate consoles such as ConEmu are implemented. Commented Aug 20, 2018 at 14:03
  • For Windows 10, Microsoft plans to release a new pseudoconsole feature in the Fall. On the Python side, support for lpAttributeList in the subprocess module (already added in 3.7) will need to be extended to support PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE. Maybe CreatePseudoConsole and creating the unbuffered pipe FDs can be wrapped as pty.openpc(size, flags) -> (fdin, fdout, hpc). Commented Aug 20, 2018 at 14:05

2 Answers 2

1

This does not use "Command Prompt" as in the program included with Windows, but ConEmu has an -insidewnd parameter which is meant for allowing it to be embedded in third-party applications.

import sys,pathlib,subprocess
import tkinter as tk
import tkinter.ttk as ttk

# assuming a common location for the portable conemu installation:
conemu_exe = pathlib.Path.home()/"Documents"/"ConEmu"/"ConEmu64.exe"

class app(tk.Tk):
    def __init__(self):
        super().__init__()
        self.console_frame = ttk.Frame(self,height=480,width=640)
        self.console_frame.pack(fill="both",expand=True)
        hwnd = hex(self.console_frame.winfo_id())
        p = subprocess.Popen(
            [str(conemu_exe), "-insidewnd", hwnd],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE)

app().mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

This is an excellent solution, shame you can't do it with cmd.
0
from tkinter import Tk, Text
import subprocess


def execute(code):
    command = cmd.get('1.0', 'end').split('\n')[-2]
    if command == 'exit':
        exit()
    cmd.insert('end', f'\n{subprocess.getoutput(command)}')


main = Tk()

cmd = Text(main)
cmd.pack()

cmd.bind('<Return>', execute)

main.mainloop()

1 Comment

You should give some kind of explanation how your code solves the issue and answers the question. In that way your answer will improve a lot in quality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.