Tkinter框架内的Windows“命令提示符”

时间:2018-08-20 11:05:04

标签: windows python-3.x tkinter command-line subprocess

如何在tkinter框架内运行命令提示符?我知道如何使用“ pygame”执行此操作,但这些方法不起作用-命令提示符仅在另一个窗口中运行。

我试图这样做:

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()

但是,正如我所说,这是行不通的。

1 个答案:

答案 0 :(得分: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()