Tkinter and GUI Development

Tkinter, python's standard GUI library can be found with every installed version of python. The current library includes objects, functions and attributes used for constructing applications you see across the web, adding buttons, slide-bars, labels etc.

Basic Structure and Initialization

from Tkinter import *
  class PythonSide:
    def __init__(self, master):
      self.label = Label(master,text='PythonSide')
      self.button = Button(master, text='Click',command=self.hey)

    def hey(self):
      print 'Hello'

  main = Tk()
  obj = PythonSide(main)

Details now.
  • from Tkinter import * this line represents the call of the module Tkinter, while importing all objects and functions stored, using the star sign * = 'all'. The proper explanation of this line will be something like: "From the module 'Tkinter', find and import EVERYTHING.".

  • class Pythonside is the name of the class.

  • def __init__(self, master) indicates an initialization function which will be activated the moment we create an object (self) assigning it to the class. (master) is responsible for initiating the main window of our application throughout the mainloop.

  • self.label basically adds a label inside our object's area. Label(master,text='PythonSide') is an object/function from the Tkinter module which will display some text inside our app's environment. We assign the window to it (master) and the text with (text='PythonSide').

  • self.label.pack(side='left') is reference to position of the label. pack() will set the text on the center of the app's window, pack(side='left') to the left-side and pack(side='right'), right-side etc. Some other functions for positioning:
    • pack() or pack(side='') --> 'top', 'bottom', 'left', or 'right'

    • place(x=23, y=65) ---> x, y {Any number}

    • grid(row=1, column=3) ----> row='', column='' {Integers}

  • ...command=self.hey this attribute it's similar to the JavaScript function 'onclick' and comes with the button object of Tkinter. The moment you click the button, the function self.hey must be in place and written correctly. If doesn't exist, an AttributeError will be raised. So make sure you have the function def hey(self) defined!.

  • main = Tk() is the TopLevel widget. It's necessary for the appearance of the main window.

  • obj = PythonSide(main) As we've discussed on the section classes, each class needs an object to inherit and access its functions. So we create an object(obj) assigning it to the class we've made (PythonSide) which by itself includes the TopLevel widget (main).

  • main.mainloop(). Early lessons of OpenGL discuss that for an application window to stay open, a series of drawing functions will take place every millisecond (if not faster). These functions give us the impression that the window stays open, when in reality it's not. Think something like a very very fast flickering action. This line basically says "run an infinite loop" (mainloop()) (of these drawing functions) for the window (main). If we cut the mainloop() the window will disappear. Makes sense right?!

  • exercise-icon Try it!

    Assuming class PythonSide (from above) write the line of adding a label named labelTwo with text='I am label two'. Position it using grid function with row=3 and column=4.


    exercise-icon Try it!

    Assuming class PythonSide (from above) write the line of adding a button named buttonNew with text='Click Me' and command a function self.dataExtract. Position it using place function with x=45 and y=128.

    ? | Contact Me | © 2016