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.

greenbeaf
  • 177
  • 1
  • 2
  • 9
  • This has been asked several times. – Mike - SMT Aug 20 '18 at 12:59
  • Possible duplicate of [Redirect command line results to a tkinter GUI](https://stackoverflow.com/questions/665566/redirect-command-line-results-to-a-tkinter-gui) – Mike - SMT Aug 20 '18 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. – Eryk Sun Aug 20 '18 at 14:03
  • For Windows 10, Microsoft plans to release a new [pseudoconsole feature](https://blogs.msdn.microsoft.com/commandline/2018/08/02/windows-command-line-introducing-the-windows-pseudo-console-conpty) 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)`. – Eryk Sun Aug 20 '18 at 14:05

2 Answers2

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()
dusty
  • 865
  • 9
  • 15
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()
Pavan Sai
  • 127
  • 2
  • 11
  • 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. – programandoconro May 17 '21 at 11:14