0

(Using Ubuntu) I'm trying to create a GUI with GTK3+ using python and Glade, the objective is to automate my daily activites in job. Basically I need to make the buttons in the GUI execute the commands that i usually hand-write in terminal when clicked, and it's important to get the output of the commands to send some infos to the GUI.

So I'm struggling to understand how can I run multiple terminals in background and send their outputs to the script running the GTK interface.

Jobson
  • 1

1 Answers1

0

to execute shell script commands and get the output you really don't need the terminal you could do something like "subprocess.check_output" which will execute the command and get the output... after getting the output you could print it in a text view

class LabelWindow(Gtk.Window):
    def on_clicked(self, button , data):
        result = subprocess.check_output(['ls'])
        data.set_text(result)
 



 def __init__(self):
    Gtk.Window.__init__(self, title="Label Example")

    hbox = Gtk.Box(spacing=10)
    hbox.set_homogeneous(False)
    vbox_left = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
    vbox_left.set_homogeneous(False)
    vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
    vbox_right.set_homogeneous(False)

    hbox.pack_start(vbox_left, True, True, 0)
    hbox.pack_start(vbox_right, True, True, 0)

    label = Gtk.Label(label="This is a normal label")
    vbox_left.pack_start(label, True, True, 0)

    button = Gtk.Button(label="Click at your own risk")
    label.set_mnemonic_widget(button)
    vbox_right.pack_start(button, True, True, 0)
    button.connect("clicked", self.on_clicked, label)

    self.add(hbox)


window = LabelWindow()
window.connect("destroy", Gtk.main_quit)
window.show_all()
Gtk.main()

this is a very rough example.. but if each button has will execute a command synchronously like one after another then you could create more button and add the specific command to each of em. if you want them to be executed asynchronously like one should not wait for another means add threads and use glib.idle_add Python GTK+ 3 Safe Threading

Siva Guru
  • 694
  • 4
  • 12
  • But in the case that i need to get the outputs of the subprocess in real-time, line by line, of a program that runs for indefinite time how can i do that? – Jobson May 21 '21 at 20:49
  • you just have to redirect the output of that process to your program when you run the command you will get the stdout.. just read from it and print it in your application it will be like real time reading – Siva Guru May 22 '21 at 18:47