GUI In Python Using PyGTK

Linux tutorials and code.
1 post Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

GUI In Python Using PyGTK
mikethedj4
Well I showed you how you can create a GUI in C here, but I haven't showed you in Python. So that's what we're going to do today.

For a better understanding of what does what go hit up GTK+ for Python. (This may also help you too.)

Now with that being said all will need for this project is a test editor, and your terminal, but will open up our terminal later as we don't need it now.

Lets begin by importing what we need to begin the gui.
Code: Select all
#!/usr/bin/env python

import gtk
NOTE: Everytime you begin making a Python program you haft to add #!/usr/bin/env python.

Now the code to create the GUI.
Code: Select all
class app:

  def __init__(self):
	window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	window.set_position(gtk.WIN_POS_CENTER)
	window.set_title("TestApp")
	window.set_default_size(320, 200)
	window.connect("destroy", gtk.main_quit)
	window.show_all()

app()
gtk.main()
NOTE: You can name the class "app" anything you want.

Here's the code you'd use to give the app an icon, once running...
Code: Select all
gtk.window_set_default_icon_from_file('file_here.png')
Here's the code to have your window in Fullscreen.
Code: Select all
self.window.fullscreen()
Here's the code to adjust the main window's opacity. (It's set at 50%)
Code: Select all
self.window.set_opacity (0.5);
If you want to adjust the position from when your window starts up. The code below does exactly that. (I have it set to center)
Code: Select all
window.set_position(gtk.WIN_POS_CENTER)
If you want to remove the title bar. You would use the following code.
Code: Select all
window.set_decorated(False)
window.set_has_frame(False)
To hide the program from being visible in the taskbar/panel. You should use the following code.
Code: Select all
window.set_property('skip-taskbar-hint', True)
Here's the code to change your window's background color.
Code: Select all
color = gtk.gdk.color_parse('#1200ff')
window.modify_bg(gtk.STATE_NORMAL, color)
The last one I'm gonna go over is. If you want your window to display above all other windows (aka TopMost/Pinned) you would use the following code.
Code: Select all
window.set_keep_above(True)
So above you have pretty much the basic codes on configuring your window. I'll leave adding widgets (buttons, and such) to yourself. However quick note though. You will need to add vertical box's, and/or horizontal box's to position your buttons, textbox's, etc:

Now after all that here's the full code, for our basic window.
Code: Select all
#!/usr/bin/env python

import gtk

class app:

   def __init__(self):
	window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	window.set_position(gtk.WIN_POS_CENTER)
	window.set_title("TestApp")
	window.set_default_size(320, 200)
	window.connect("destroy", gtk.main_quit)
	window.show_all()

app()
gtk.main()
Oh before I forget!

Some cases you may wanna hide/change your cursor. For this process you may wanna take a look at gtk.gdk.Window as well as gtk.gdk.Cursor.

Below is the exact same source of our application above, except I hid the cursor, and like I said before. If you need any help with the cursors please refer back to the two links I put down above, as they'll help quite a lot.
Code: Select all
#!/usr/bin/env python

import gtk

class app:

  def __init__(self):
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("TestApp")
    window.set_default_size(320,200)
    window.connect("realize", self.realize_cb)
    window.connect("destroy", gtk.main_quit)    
    window.show_all()

  def realize_cb(self, widget):
    pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
    color = gtk.gdk.Color()
    cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
    widget.window.set_cursor(cursor)

app()
gtk.main()
You do not have the required permissions to view the files attached to this post.
1 post Page 1 of 1
Return to “Programming”