GUI In Python Using PyGTK
Linux tutorials and code.
1 post
Page 1 of 1
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.
Now the code to create the GUI.
Here's the code you'd use to give the app an icon, once running...
Now after all that here's the full code, for our basic window.
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.
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
NOTE: Everytime you begin making a Python program you haft to add #!/usr/bin/env python.#!/usr/bin/env python
import gtk
Now the code to create the GUI.
Code: Select all
NOTE: You can name the class "app" anything you want.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()
Here's the code you'd use to give the app an icon, once running...
Code: Select all
Here's the code to have your window in Fullscreen.
gtk.window_set_default_icon_from_file('file_here.png')
Code: Select all
Here's the code to adjust the main window's opacity. (It's set at 50%)
self.window.fullscreen()
Code: Select all
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)
self.window.set_opacity (0.5);
Code: Select all
If you want to remove the title bar. You would use the following code.
window.set_position(gtk.WIN_POS_CENTER)
Code: Select all
To hide the program from being visible in the taskbar/panel. You should use the following code.
window.set_decorated(False)
window.set_has_frame(False)
Code: Select all
Here's the code to change your window's background color.
window.set_property('skip-taskbar-hint', True)
Code: Select all
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.
color = gtk.gdk.color_parse('#1200ff')
window.modify_bg(gtk.STATE_NORMAL, color)
Code: Select all
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:window.set_keep_above(True)
Now after all that here's the full code, for our basic window.
Code: Select all
Oh before I forget!#!/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()
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023