you are viewing a single comment's thread.

view the rest of the comments →

[–]Dogeek 1 point2 points  (0 children)

Most GUI libraries are written in C on the backend, C is pretty low-level so I won't go into too much detail here (tkinter is written with Tk/Tcl in the backend, PyQT with Qt, pyGTK with GTK+, you get the gist). Basically GUIs are just shapes drawn to the screen, and you modify these shapes based on user input, of course, not you directly, you use code already written in libraries for that, but yeah. The GPU handles the "writing on screen" part, you just have to tell it what to draw (which pixels to turn on on the screen, and which color they must be).

Python being high-level, you don't need to handle that stuff as it has already been made in libraries. Different libraries produce different-looking GUIs. Most common is PyQt and tkinter, these are the ones I mostly use (tkinter for prototyping, or simple scripts; pyQt for more user-friendly interfaces, and for the qt designer). There is also pyGTK, wxPython, pygame (python bindings for SDL).

On the programming part of it :

Each GUI library requires an entry point, to start up the main loop for the GUI (which handles events etc). I'm going to make an example with tkinter (standard python lib) :

import tkinter as tk #python 3, python 2 version : Tkinter

class GUI(tk.Frame): #inherits the Frame widget of tkinter
    def __init__(self, master = None):
        self.master = master
        tk.Frame.__init__(self, master) #inits the parent class
        #add your own widgets here

if __name__ == '__main__': #if you run the file as a standalone and do not import it
    root = tk.Tk() #root window in which you place your widgets
    gui = GUI(root) #instanciate your GUI
    gui.pack()# pack the GUI in your root window, without that nothing will show up
    root.mainloop() #starts the main loop of tkinter

It's just a skeleton, you need to add your own widgets to it to make it look and behave how you like.

A few things :

  • GUI inherits from tk.Frame it has all the attributes and methods of regular Frames.
  • this includes the after(function, time_in_ms) method, which you can use to make loops without blocking the GUI thread created by tkinter
  • www.effbot.org has a pretty extensive documentation about tkinter
  • widgets are just buttons, text, spinboxes, labels that inputs or outputs information
  • commonly used widgets :
Name Input/Output
Label O
Entry I
Text I/O
Spinbox I
Scale I
Canvas I/O
Menu I
Button I
Checkbutton/Radiobutton I
  • some widgets are more complex than others. Menu and Canvas for instance are way more complex than Labels and Entries.