GUI Controls In Python

In Python, we can create GUI controls using various libraries listed below, 
  • Tkinter  (python gui toolkit)
  • jpython (provide access of java class libraries in python)
  • wxPython (open source toolkit for wxwindow)
In this article we  will focus on Tkinter because tkinter is very easy to use for creating GUI controls and provides object oriented interfaces. Before creating controls,we need to create window form (we used IDLE python 3.5), open a new file to written our source code.(shown in picture below).

 

Now the file is open and we will iwrite our code in this file, let's go.
 
Before creating controls, we create window form, 
  1. from tkinter import* #import tkinter module
  2. w=Tk() #creating window form
  3. w.geometry("600x600") #set hight and width of window form
  4. w.title("controls in python") # set tile of windows form
our window form created, we add(create) controls in our windows form
 
Button
 
Button are the tkinter widgets that allow to perform user define funtion on window form like click event  or execute function on button click.
  1. b=Button(w,text="say hello").pack() #creating button  
Our button is created on our windows form. 
 
Check Button
 
Check button is a gui control that is used in performing operations on user input and also used for input fixed input form user,
  1. C1 = Checkbutton(w, text = "Music",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button  
  2. C2 = Checkbutton(w, text = "Video",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button  
Check button is created on our window form,
 
Entry  & Label
 
Entry is just like a text boxt, entry is used for  input and output. 
 
Label is used for text on windows form,
  1. L1 = Label(w, text="User Name").pack() #label  
  2. E1 = Entry(w).pack() # entry  
entry and label are created. 
 
List Box
 
The Listbox widget is used to provide a list of options to a user,
  1. L2 = Label(w, text="list box").pack()  #label  
  2. Lb1 = Listbox(w)   #list box  
  3. Lb1.insert(1, "Python")         # adding value into list box  
  4. Lb1.insert(2, "c# corner")     # adding value into list box  
  5. Lb1.insert(3, "C")         # adding value into list box  
  6. Lb1.insert(4, "PHP")   # adding value into list box  
  7. Lb1.insert(5, "C#")      # adding value into list box  
  8. Lb1.insert(6, "asp")    # adding value into list box  
  9. Lb1.pack()  
list box is created.
 
Menu Button
 
The Menubutton widget is used to display menus in our application.
  1. mb= Menubutton ( w, text="click me", relief=RAISED )    
  2. mb.menu = Menu ( mb, tearoff = 0 )    
  3. mb["menu"] = mb.menu    
  4. a = IntVar()    
  5. b = IntVar()    
  6. mb.menu.add_checkbutton ( label="AJAY",variable=a )    
  7. mb.menu.add_checkbutton ( label="MALIK",variable=b )    
  8. mb.pack()    
Menubuttion is created.
 
Message 
 
Is just used for dispaly text on window form, 
  1. var = StringVar()  
  2. label = Message( w, textvariable=var, relief=RAISED )  
  3. var.set("Hey!? How are you doing?")  
  4. label.pack()  
Message is created
 
Spinbox 
 
Spinbox widget is Tkinter Entry widget, which can be used to select from a fixed number of values.
  1. s = Spinbox(w, from_=0, to=10)  
  2. s.pack()  
Spinbox is created. 
 
Canvas
 
The Canvas is a rectangular area for drawing pictures or other controls. You can place graphics, text, widgets or frames on a Canvas.
  1. C = Canvas(w, bg="blue", height=250, width=300) # bg for color   
  2. coord = 10, 50, 240, 210  
  3. arc = C.create_arc(coord, start=0, extent=150, fill="red")  
  4. C.pack()  
Canvas is created. 
 
Finally we created some GUI controls and our source code for all controls are:
  1. from tkinter import* #import tkinter module  
  2. w=Tk() #creating window form  
  3. w.geometry("600x600") #set hight and width of window form  
  4. w.title("controls in python") # set tile of windows form  
  5. b=Button(w,text="say hello") #creating button  
  6. b.pack()  
  7. C1 = Checkbutton(w, text = "python",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button  
  8. C2 = Checkbutton(w, text = "C# corner",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button  
  9. L1 = Label(w, text="User Name").pack() #label  
  10. E1 = Entry(w).pack() # entry  
  11. L2 = Label(w, text="list box").pack() #label  
  12. Lb1 = Listbox(w) #list box  
  13. Lb1.insert(1, "Python") # adding value into list box  
  14. Lb1.insert(2, "Perl") # adding value into list box  
  15. Lb1.insert(3, "C") # adding value into list box  
  16. Lb1.insert(4, "PHP") # adding value into list box  
  17. Lb1.insert(5, "JSP") # adding value into list box  
  18. Lb1.insert(6, "Ruby") # adding value into list box  
  19. Lb1.pack()  
  20. mb= Menubutton ( w, text="click me", relief=RAISED )  
  21. mb.menu = Menu ( mb, tearoff = 0 )  
  22. mb["menu"] = mb.menu  
  23. a = IntVar()  
  24. b = IntVar()  
  25. mb.menu.add_checkbutton ( label="AJAY",variable=a )  
  26. mb.menu.add_checkbutton ( label="MALIK",variable=b )  
  27. mb.pack()  
  28. var = StringVar()  
  29. label = Message( w, textvariable=var, relief=RAISED )  
  30. var.set("Hey!? How are you doing?")  
  31. label.pack()  
  32. s = Spinbox(w, from_=0, to=10)  
  33. s.pack()  
  34. C = Canvas(w, bg="blue", height=250, width=300)  
  35. coord = 10, 50, 240, 210  
  36. arc = C.create_arc(coord, start=0, extent=150, fill="red")  
  37. C.pack()  
  38. w.mainloop()  
Now run our application (pressed F5). Output is shown in the screenshot below:
 
 
 
Conclusion 

We learnt in this article how to create controls, later on we used these controls and will use their attributes in further articles.

Up Next
    Ebook Download
    View all
    Learn
    View all