Graphical User Interfaces

There are several GUI frameworks for Python. Some examples:

One approach to Graphic User Interfaces is to use the Tkinter library that is used in the rest of this page.

We first create a class containing a functional description of the user interface elements and then an create instance of that class. Here is an example class that creates 3 buttons. It has a constructor __init__() and a function create_widgets().

class Application(Frame):
  """ A GUI app """

  def __init__(self,master):
    """ Constructor: init the frame """
    Frame.__init__(self,master)
    self.grid()
    self.button_clicks=0
    self.create_widgets()

  def create_widgets(self):
    self.button1 =  Button(self, text="Button 1")
    self.button1.grid()

    self.button2 =  Button(self, text="Button 2")
    self.button2.grid()

    self.button3 =  Button(self, text="Button 3")
    self.button3.grid()

In the code below we create a root window, give it some properties like a title and dimensions and then create an instance of the class given above. Then, to make the GUI receive input events and respond to them, the function mainloop() is called on the root window.

# create a window
root = Tk()

# set window props
root.title("ME Gui")
root.geometry("300x200")

app = Application(root)

root.mainloop()

Here is the entire program: gui_3_buttons.py

If all you want is a pop-up box, a simpler way is to use a message box. This is described here: message box

Sliders

In Tkinter, sliders are called Scales. Here is a simplified way to create and configure a slider:
self.slider1 = Scale(self)
self.slider1.config(orient=HORIZONTAL)
self.slider1.config(length=400)
self.slider1.config(width=10)
self.slider1.config(sliderlength=20)
self.slider1.config(from_=0)
self.slider1.config(to_=1000)
self.slider1.config(tickinterval=200)

Here is a functional program: slider.py