Showing posts with label python gui programming. Show all posts
Showing posts with label python gui programming. Show all posts

Wednesday, July 29, 2015

Python Tkinter Programming

Desktop GUI application creation in an easy mode is one of the specialty of the python programming. Python 2.7 and Python 3 both have an inbuilt class tkinter which is needed to program graphical interface in python. There are other modules as well but here we will focus only on tkinter.

A basic tkinter program to show 'Hello World' in GUI mode -

from Tkinter import *
# create a root window
root = Tk()
root.title("Lazy Buttons")
root.geometry("400x120")
lbl=Label(root,text="Hello World").pack()

root.mainloop()

If this shows error, change the Tkinter to tkinter in the import statement. After importing the module we create the root window by defining is as the Tk() object. In the next statement we named the window as "Lazy Buttons" by calling the root.title() method.
root.geometry() is used to define the width and height of the window. It has a formal notation like root.geometry("w x h+ x+ y") where w and h are for width and height respectively and the x and y are the coordinates of the screen.

In the next statement we created a Label, which will show the "Hello World" texts to be displayed  in the root window. The Label creator method takes the window name as input i.e. root and the text as input.The pack() method is needed to pack the label to the root window. The mainloop is needed to make the window to exist forever until you click the cross button.
Buy One Plus Two without Registration

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...