【问题标题】:Is there any way to open git-bash and send commands in Python?有没有办法在 Python 中打开 git-bash 并发送命令?
【发布时间】:2023-03-03 17:56:01
【问题描述】:
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命令。

【问题讨论】:

    标签: python tkinter terminal


    【解决方案1】:

    要做到这一点,你可以使用一个名为subprocess的模块

    import subprocess
    
    subprocess.run("ipconfig", shell=True, capture_output=True)
    

    这段代码将运行ipconfig,它会在shell中运行,输出也会被捕获

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2015-12-23
      • 2016-06-19
      • 1970-01-01
      • 2020-01-03
      相关资源
      最近更新 更多