使用 python gtk 执行终端命令

Execute terminal commands using python gtk

我想使用 gtk 和 python 制作一个快速应用程序,当单击按钮时它会执行终端命令。不管怎样,我能做到吗?

您可以使用 Python 执行 this tutorial for Python and Gtk. The second example in the tutorial shows how to create a window with a button so to run a command you just need to change the on_button_clicked callback. You can use either os.system or the subprocess module 至 运行 个命令。

所以以运行ls为例:

import subprocess
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")

        self.button = Gtk.Button(label="Click Here")
        self.button.connect("clicked", self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        subprocess.run(["ls"])


win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()