如何在Tkinter小部件中获取cmd [英] How to get cmd in Tkinter widget

查看:70
本文介绍了如何在Tkinter小部件中获取cmd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Tkinter GUI,并希望将Windows CMD添加到tkinter小部件中.我想使用控制台连接到数据库.我做了一些研究,发现只有pyconsole模块,但是有一些错误:

I'm creating Tkinter GUI and want to add windows CMD into tkinter widget. I would like to use console to connect to database. I did some research and found only pyconsole module, but with some bugs:

  • cls不会做您期望的事情;
  • 编辑将不显示编辑器(而是尝试开始编辑);
  • 提示一切失败;
  • 未执行color命令;
  • 也不支持出色的^ C(它实际上是在复制文本,而不是中断进程).

特别是省略了^ C命令,这对我要运行的sql脚本来说是一个巨大的限制.

Especially ^C command ommited is huge limitation for sql scripts i want to run.

我可以这样打开控制台:

I'm able to open console like this:

Popen(["cmd.exe"], creationflags=CREATE_NEW_CONSOLE)

但是使用这种方法,我不知道如何与GUI交互(甚至有可能吗?)

But with this approach I don't know how to interact with the GUI (is it even possible?)

我的Text小部件也可以从命令行读取输出,但是我还需要在该命令行中编写,而不仅仅是读取它...

Also my Text widget can read output from command line, but I need to also write in that command line, not just read it...

是否有可能将常规CMD放入Tkinter小部件中,从而与GUI中的其他小部件进行反应?

Is there a possibility to get regular CMD into Tkinter widget, which will react with the rest of widgets in GUI?

所需的行为将是右侧的CMD控制台,如下面的图片所示(在tkinter窗口中),它将与左侧的列表框进行交互.我不是在寻找确切的代码(这就是为什么我的代码不在这里说明),而是方法/解决方案如何将CMD放入tkinter.

Desired behaviour would be CMD console on the right side as you can see on picture below (in tkinter window), that would interact with the Listbox on the left. I'm not looking for exact code (that's why no my code stated here), but method/solution how to put CMD into tkinter.

照片:

谢谢

Honza

推荐答案

我认为您可以使用Entry输入要执行的命令.然后,您可以使用 subprocess.run subprocess.Popen 执行命令,并使用Text甚至更好的 tkinter.scrolledtext.ScrolledText 小部件显示结果.

I think you can use an Entry to input the commands you want to execute. Then you can use subprocess.run and subprocess.Popen to execute the commands, and a Text or even better a tkinter.scrolledtext.ScrolledText widget to show the results.

这篇关于如何在Tkinter小部件中获取cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆