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 write our source code. (shown in the picture below).
Now the file is open and we will write our code in this file, let's go.
- from tkinter import* #import tkinter module
- w=Tk() #creating window form
- w.geometry("600x600") #set hight and width of window form
- w.title("controls in python") # set tile of windows form
our window form created, we add(create) controls in our windows form
Button
The button is the tkinter widgets that allow performing users to define a function on window form like click event or execute a function on button click.
- 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,
- C1 = Checkbutton(w, text = "Music",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button
- 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 box, entry is used for input and output.
Label is used for text on windows form,
- L1 = Label(w, text="User Name").pack() #label
- 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,
- L2 = Label(w, text="list box").pack() #label
- Lb1 = Listbox(w) #list box
- Lb1.insert(1, "Python") # adding value into list box
- Lb1.insert(2, "c# corner") # adding value into list box
- Lb1.insert(3, "C") # adding value into list box
- Lb1.insert(4, "PHP") # adding value into list box
- Lb1.insert(5, "C#") # adding value into list box
- Lb1.insert(6, "asp") # adding value into list box
- Lb1.pack()
Menu Button
The Menubutton widget is used to display menus in our application.
- mb= Menubutton ( w, text="click me", relief=RAISED )
- mb.menu = Menu ( mb, tearoff = 0 )
- mb["menu"] = mb.menu
- a = IntVar()
- b = IntVar()
- mb.menu.add_checkbutton ( label="AJAY",variable=a )
- mb.menu.add_checkbutton ( label="MALIK",variable=b )
- mb.pack()
Menubuttion is created.
Message
Is just used for dispaly text on window form,
- var = StringVar()
- label = Message( w, textvariable=var, relief=RAISED )
- var.set("Hey!? How are you doing?")
- label.pack()
Message is created
Spinbox
Spinbox widget is Tkinter Entry widget, which can be used to select from a fixed number of values.
- s = Spinbox(w, from_=0, to=10)
- s.pack()
Canvas
The Canvas is a rectangular area for drawing pictures or other controls. You can place graphics, text, widgets or frames on a Canvas.
- C = Canvas(w, bg="blue", height=250, width=300) # bg for color
- coord = 10, 50, 240, 210
- arc = C.create_arc(coord, start=0, extent=150, fill="red")
- C.pack()
Canvas is created.
Finally we created some GUI controls and our source code for all controls are:
- from tkinter import* #import tkinter module
- w=Tk() #creating window form
- w.geometry("600x600") #set hight and width of window form
- w.title("controls in python") # set tile of windows form
- b=Button(w,text="say hello") #creating button
- b.pack()
- C1 = Checkbutton(w, text = "python",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button
- C2 = Checkbutton(w, text = "C# corner",onvalue = 1, offvalue = 0, height=2,width = 20).pack()#check button
- L1 = Label(w, text="User Name").pack() #label
- E1 = Entry(w).pack() # entry
- L2 = Label(w, text="list box").pack() #label
- Lb1 = Listbox(w) #list box
- Lb1.insert(1, "Python") # adding value into list box
- Lb1.insert(2, "Perl") # adding value into list box
- Lb1.insert(3, "C") # adding value into list box
- Lb1.insert(4, "PHP") # adding value into list box
- Lb1.insert(5, "JSP") # adding value into list box
- Lb1.insert(6, "Ruby") # adding value into list box
- Lb1.pack()
- mb= Menubutton ( w, text="click me", relief=RAISED )
- mb.menu = Menu ( mb, tearoff = 0 )
- mb["menu"] = mb.menu
- a = IntVar()
- b = IntVar()
- mb.menu.add_checkbutton ( label="AJAY",variable=a )
- mb.menu.add_checkbutton ( label="MALIK",variable=b )
- mb.pack()
- var = StringVar()
- label = Message( w, textvariable=var, relief=RAISED )
- var.set("Hey!? How are you doing?")
- label.pack()
- s = Spinbox(w, from_=0, to=10)
- s.pack()
- C = Canvas(w, bg="blue", height=250, width=300)
- coord = 10, 50, 240, 210
- arc = C.create_arc(coord, start=0, extent=150, fill="red")
- C.pack()
- w.mainloop()
Now run our application (pressed F5). Output is shown in the screenshot below:
Conclusion
We learned in this article how to create controls, later on, we used these controls and will use their attributes in further articles.