python - 有没有办法在 Python 中打开 git-bash 并发送命令?
问题描述
from tkinter import *
import os
pencere = Tk()
pencere.title("Hello")
pencere.geometry("550x550")
label1 = Label(text="Hallo",font="bold 25")
label1.place(x=150,y=30)
label2 = Label(text="Command")
label2.place(x=5,y=90)
label3 = Entry(width=50)
label3.place(x=50,y=90)
def gitbash():
os.startfile('C:\\Program Files\\Git\\git-bash.exe')
ac = Button(text="Olustur",width=10,height=3,command = cmd)
ac.place(x=100,y=150)
pencere = mainloop()
我想从 gitbash 函数向 GitBash 发送命令。例如我想发送“ls”命令,当我打开 Button 时,应该打开 GitBash 并发送 ls 命令。
解决方案
为此,您可以使用一个名为subprocess
import subprocess
subprocess.run("ipconfig", shell=True, capture_output=True)
这段代码将运行ipconfig
,它将在 shell 中运行,并且输出也将被捕获
推荐阅读
- sql - 如何按 SDO_GEOMETRY 数据类型分组
- django - 如何使用 save_to_database() 跳过一些在 django-excel 中有错误的行
- c++ - OpenCascade 未定义的函数参考
- tensorflow - 在 Keras 中,ResNet50 有一个奇怪的模式
- laravel - Axios 重新加载 PDF 内容
- amazon-ec2 - 您是否需要在 Openpvn config 中添加额外的代理设置:从代理服务器后面的设备向 AWS 实例发送数据?
- javascript - 将兆字节转换为千兆字节
- javascript - 如何从 HTML 模式对话框中获取函数返回
- sql - 如何通过自动创建 id 每天将表 1 插入表 2 作为每日日期从表 1 id = 1 到表 2?
- reactjs - NextJS Typescript - 保护后对象可能未定义?