Show terminal output in a gui window using python Gtk

python gtk3 windows
gtk window python example
gtk3 tutorial
python gtk grid example
gtk python widgets
python gtk centos
gtk+3
pygtk required

I am developing a software and in that I want a window, that will display the output thrown by the terminal (like a package manager does ). Example, if i give the install command, the installation process should be outptutted to my window and not the terminal. Is there a way to do this in python Gtk?

If you are on Linux (as you state), something like this should work:

import gtk 
import gobject
import pango
import os
from subprocess import Popen, PIPE
import fcntl

wnd = gtk.Window()
wnd.set_default_size(400, 400)
wnd.connect("destroy", gtk.main_quit)
textview = gtk.TextView()
fontdesc = pango.FontDescription("monospace")
textview.modify_font(fontdesc)
scroll = gtk.ScrolledWindow()
scroll.add(textview)
exp = gtk.Expander("Details")
exp.add(scroll)
wnd.add(exp)
wnd.show_all()
sub_proc = Popen("ping -c 10 localhost", stdout=PIPE, shell=True)
sub_outp = ""


def non_block_read(output):
    fd = output.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    try:
        return output.read().decode("utf-8")
    except:
        return ''


def update_terminal():
    textview.get_buffer().insert_at_cursor(non_block_read(sub_proc.stdout))
    return sub_proc.poll() is None

gobject.timeout_add(100, update_terminal)
gtk.main()

The nonblocking read idea is from here.

Using a Label to display the text:

import gtk 
import gobject
import os
from subprocess import Popen, PIPE
import fcntl

wnd = gtk.Window()
wnd.set_default_size(400, 400)
wnd.connect("destroy", gtk.main_quit)
label = gtk.Label()
label.set_alignment(0, 0)
wnd.add(label)
wnd.show_all()
sub_proc = Popen("ping -c 10 localhost", stdout=PIPE, shell=True)
sub_outp = ""


def non_block_read(output):
    ''' even in a thread, a normal read with block until the buffer is full '''
    fd = output.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    try:
        return output.read().decode("utf-8")
    except:
        return ''


def update_terminal():
    label.set_text(label.get_text() + non_block_read(sub_proc.stdout))
    return sub_proc.poll() is None

gobject.timeout_add(100, update_terminal)
gtk.main()

The Python GTK+ 3 Tutorial, GUI programming introduces new problems compared to interacting with the standard output (console / terminal). It is necessary for you to  In this tutorial, we will create a dialog which takes input from the user and prints it in the terminal, the purpose of this tutorial is to understand how to take the user input for GUI application. We will use the built-in Python package Tkinter it is implemented as a Python wrapper for the Tcl Interpreter embedded within the interpreter of Python. Creating User Input Dialog With Tkinter

You can use the subprocess module and the os module to get back the terminal output.You can check this question.

Developing Programming Courses with Moodle and VPL, Python. graphic. Application. Create a new VPL assignment and paste the Open window and go into main loop root. mainloop () The program above is a very simple graphic Python program that uses the Python GTK implementation tkInter to open a 200x200 GUI with This should produce the output shown in figure 7.6. The Gtk.Window is a simple container. win.show_all() We can show all widgets in one shot with show_all() method. Figure: Gtk.Label Python Gtk.Image. Gtk.Image dispalys an image. If the file is not loaded successfully, the image will contain a "broken image" icon.

I've been trying to do this and really struggling. I couldn't get the first example working and moved quickly onto the 2nd.

Using the 2nd example above (Using Label to display text) and found that this works fine with Python 2.7 but I'm trying to use Python3 and some things just don't work.

I struggled for ages trying to convert to Python3 and had to change some of the imports and change gtk to Gtk which got it mostly working but the thing that really stumped me was due to Python3 using utf-8 codes. I finally got it working by modifying "the non_block_read" function changing the returned text from utf-8 to string and coping with the return None case.

I hope this helps.

For completeness I attach my working code:-

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk
from gi.repository import GObject

import os
from subprocess import Popen, PIPE
import fcntl

wnd = Gtk.Window()
wnd.set_default_size(400, 400)
wnd.connect("destroy", Gtk.main_quit)
label = Gtk.Label()
label.set_alignment(0, 0)
wnd.add(label)
wnd.show_all()
sub_proc = Popen("ping -c 10 localhost", stdout=PIPE, shell=True)
sub_outp = ""


def non_block_read(output):
    ''' even in a thread, a normal read with block until the buffer is full '''
    fd = output.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    op = output.read()
    if op == None:
        return ''
    return op.decode('utf-8')



def update_terminal():
    label.set_text(label.get_text() + non_block_read(sub_proc.stdout))
    return sub_proc.poll() is None

GObject.timeout_add(100, update_terminal)
Gtk.main()

A Beginner's Guide to Using pyGTK and Glade, It is fair to say that for a small company, using pyGTK over other GUI files wherever a button or an entry box or an output text buffer is located. window (​go to the main Glade window and click View→Show Properties). Clicking GO! should produce a nifty button-clicked message in your terminal window. Print output in GUI interface Tkinter Python. from Tkinter import * def printSomething(): print "Hey whatsup bro, i am doing something very interresting." root = Tk() button = Button(root, text="Print Me", command=printSomething) button.pack() root.mainloop() The output is coming in the terminal from where i am running the code I need the output in GUI interface.

PyGTK - Quick Guide, PyGTK is a set of wrappers written in Python and C for GTK + GUI library. 2D graphics with support for multiple output devices (including the X Window System​, Win32) For example, Button has a label to show text on the face. Unlike a console mode application, which is executed in a sequential manner, a GUI-​based  simple text file and output the contents to a GUI window when I click on a button. I have written a small python program to read the contents of a file when a button is clicked but can only output this to a console window. I'm using the pygtk binding with glade for the gui. I know it must be quiet simple but a mental block has rapidly descended.

PyGTK For GUI Programming/First Steps, A bug in some versions of PyGTK causes IDLE to crash when using the `gtk` module. Open a command shell/terminal, type the "python" command, hit [Enter​], and Note that, by default, this does not immediately show the window on the  This tutorial gives an introduction to writing GTK+ 3 applications in Python. Prior to working through this tutorial, it is recommended that you have a reasonable grasp of the Python programming language. GUI programming introduces new problems compared to interacting with the standard output (console / terminal).

[PDF] An introduction to GUI building with Glade, set terminal png. Define the output as being in PNG graphics format. set style data It asks us whether we want to build a “GTK+” project or a “GNOME project”​. At the The properties window will stop being greyed out and will show the mostly non- existent The Python script needs to know where Glade. Finally the window size is set using gtk_window_set_default_size and the window is then shown by GTK via gtk_widget_show_all(). When you exit the window, by for example pressing the X, the g_application_run() in the main loop returns with a number which is saved inside an integer named "status".

Comments
  • To be precise, i work in ubuntu 13.04.
  • pygabriel.wordpress.com/2009/07/27/…
  • This updates it to a textview. What if i want to send it to a Label?
  • Because a textview looks some what ugly
  • Kind of. But the actual output i want is , Like what happens in synaptic package manager. The output that it shows when you install through it
  • As far as I can tell, synaptic uses a TextView inside a ScrolledWindow and an Expander to show/hide it. Also, a fixed width font is used. I updated the first example accordingly. Of course, you can further refine the font and display styles to suit your needs.