Python Tkinter
Tkinter is Python’s standard GUI package. When combined python with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Syntax:
import tkinter as short_name
variable_name = alise_name.Tk()
variable_name.title("Title")
variable_name.mainloop()
Example
import tkinter as short_name
variable_name = alise_name.Tk()
variable_name.title("Title")
variable_name = alise_name.Tk()
variable_name.title("Title")
variable_name.mainloop()
import tkinter as tk
T= tk.Tk()
T.title("tk")
T.mainloop()
import tkinter as tk
T= tk.Tk()
T.title("tk")
T.mainloop()
Tkinter widgets
1.Label:
The Label is used to display a text or image.
The Entry is used to type a text.
The Button widget is used to trigger any actions. These actions can be navigating to another form, interaction with the dialog box, perform display operations, etc.
Syntax:
tk.Label(tkinter_variable_name,options)
tk.Label(tkinter_variable_name,options)
2.Entry:
The Entry is used to type a text.
Syntax:
tk.Entry(tkinter_variable_name,options)
tk.Entry(tkinter_variable_name,options)
3.Button:
The Button widget is used to trigger any actions. These actions can be navigating to another form, interaction with the dialog box, perform display operations, etc.
Syntax:
tk.Button(tkinter_variable_name,options)
tk.Button(tkinter_variable_name,options)
Tkinter Geometry
The Tkinter geometry is used to displaying widgets like button, list, Checkbox and so on. If set Tkinter size using geometry method :
Syntax:
tkinter_variable_name.geometry("widh x hight")
Example:
T.geometry("100x100")
1. pack() method:
- A pack() is used for displaying labels,buttons,etc in Tkinter.
- Options: Side='left', Side='right', Side='bottom',Side='top'.
- The default pack method aligns in the center.
Syntax:
widget_variable_name.pack(options)Example:
B.pack()
Example Program:
T.mainloop()
Example Program:
import tkinter as tk
T= tk.Tk()
B1 =tk.Button(T, text = "Addition", fg = "red")
B1.pack( side = 'left')
B2=tk. Button(T, text = "Subtraction", fg = "black")
B2.pack( side ='right')
B3=tk. Button(T, text = "Multiplication", fg = "blue")
B3.pack( side = 'top')
B4=tk. Button(T, text = "Division", fg = "green")
B4.pack( side = 'bottom')
T.mainloop()
Output:
2. grid() method:
widget_variable_name.grid(row=value,column=value)
Example:
L.grid(row=0,column=0)
Example Program:
import tkinter as tk
T = tk.Tk()
tk.Label(T,text = "Name").grid(row = 0, column = 0)
e1 =tk.Entry(T)
e1.grid(row = 0, column = 1)
tk.Label(T,text = "Password").grid(row = 1, column = 0)
e2 = tk.Entry(T)
e2.grid(row = 1, column = 1)
b= tk.Button(T, text = "Submit").grid(row = 4, column = 0)
T.mainloop()
T = tk.Tk()
tk.Label(T,text = "Name").grid(row = 0, column = 0)
e1 =tk.Entry(T)
e1.grid(row = 0, column = 1)
tk.Label(T,text = "Password").grid(row = 1, column = 0)
e2 = tk.Entry(T)
e2.grid(row = 1, column = 1)
b= tk.Button(T, text = "Submit").grid(row = 4, column = 0)
T.mainloop()
3.place() method
The place() geometry manager organizes the widgets to the specific x and y coordinates.
Syntax:
widget_variable_name.place(x=value,y=value)
Example:
L.place(x=30,y=50)
Example Program:
Example Program:
import tkinter as tk
T =tk.Tk()
T.geometry("400x250")
l1 = tk.Label(text = "Student Roll No").place(x = 30,y = 50)
l1 = tk.Label(text = "Student Roll No").place(x = 30,y = 50)
l2 = tk.Label(text = "Student name").place(x = 30, y = 90)
l3 = tk.Label(T, text = "Student age").place(x = 30, y = 130)
e1 = tk.Entry(T).place(x = 120, y = 50)
e1 = tk.Entry(T).place(x = 120, y = 50)
e2 = tk.Entry(T).place(x = 120, y = 90)
e3 = tk.Entry(T).place(x = 120, y = 130)
T.mainloop()
Example for label,button and entry widgets:
import tkinter as tk
T = tk.Tk()
# display lables
tk.Label(T, text="First number").grid(row=0, column=0)
tk.Label(T, text="Second Number").grid(row=1, column=0)
tk.Label(T, text="Result").grid(row=2, column=0)
# Display entry fields
e1 = tk.Entry(T)
e2 = tk.Entry(T) e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
def display():
a=int(e1.get())
b=int(e2.get())
tot=a+b
tk.Label(T, text=str(tot)).grid(row=2, column=1)
# end of display function
# button to display
button1=tk.Button(T, text="Add", bg="green", command=display)
button1.grid(row=5, column=1)
T.mainloop()
Output:
Output Explanation:
import tkinter as tk
T = tk.Tk()
# display lables
tk.Label(T, text="First number").grid(row=0, column=0)
tk.Label(T, text="Second Number").grid(row=1, column=0)
tk.Label(T, text="Result").grid(row=2, column=0)
# Display entry fields
e1 = tk.Entry(T)
e2 = tk.Entry(T) e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
def display():
a=int(e1.get())
b=int(e2.get())
tot=a+b
tk.Label(T, text=str(tot)).grid(row=2, column=1)
# end of display function
# button to display
button1=tk.Button(T, text="Add", bg="green", command=display)
button1.grid(row=5, column=1)
T.mainloop()
Output:
Output Explanation:
4.CheckBox:
The Checkbox widget is used to generate a click event for the Select or choose in the lists.
Example:
import tkinter as tk
T = tk.Tk()
T.geometry('300x300')
#display label
l = tk.Label(T, bg='white', width=50, text='Choose wanted course')
l.pack()
def selection():
if (var1.get() == 1) & (var2.get() == 0):
l.config(text='You are choosed Python course')
elif (var1.get() == 0) & (var2.get() == 1):
l.config(text='You are choosed Java course')
elif (var1.get() == 0) & (var2.get() == 0):
l.config(text='I do not want anything')
else:
l.config(text='You are choosed both')
#display checkbox
var1 = tk.IntVar()
var2 = tk.IntVar()
c1 = tk.Checkbutton(T, text='Python',variable=var1, onvalue=1, offvalue=0,
command=selection)
c1.pack()
c2 = tk.Checkbutton(T, text='Java',variable=var2, onvalue=1, offvalue=0,
command=selection)
c2.pack()
T.mainloop()
import tkinter as tk
T = tk.Tk()
T.geometry('300x300')
#display label
l = tk.Label(T, bg='white', width=50, text='Choose wanted course')
l.pack()
def selection():
if (var1.get() == 1) & (var2.get() == 0):
l.config(text='You are choosed Python course')
elif (var1.get() == 0) & (var2.get() == 1):
l.config(text='You are choosed Java course')
elif (var1.get() == 0) & (var2.get() == 0):
l.config(text='I do not want anything')
else:
l.config(text='You are choosed both')
l.config(text='You are choosed both')
#display checkbox
var1 = tk.IntVar()
var2 = tk.IntVar()
c1 = tk.Checkbutton(T, text='Python',variable=var1, onvalue=1, offvalue=0,
command=selection)
command=selection)
c1.pack()
c2 = tk.Checkbutton(T, text='Java',variable=var2, onvalue=1, offvalue=0,
command=selection)
command=selection)
c2.pack()
T.mainloop()
Program Explanations:
Step1: Import tkinter
Step2: Set label
Step3: Definitions of function
Step4: IntVar()
The IntVar() function used to assign initial values for checkbox ,i.e. off and on operation. So this function contains two values. Zero and One (0 and 1).
Step5: Display Checkbox
Output:
Output Explanation:
4.Radio Button:
The Radio button widget is used to select a single item from a predefined list of options.
Example:
Program Explanations:
Step1: Import tkinter
Step2: Set label
Step3: Definitions of function Step4: IntVar()
The IntVar() function used to assign initial values for checkbox ,i.e. off and on operation. So this function contains two values. Zero and One (0 and 1).
Step5: Display Checkbox
Step2: Set label
Step3: Definitions of function Step4: IntVar()
The IntVar() function used to assign initial values for checkbox ,i.e. off and on operation. So this function contains two values. Zero and One (0 and 1).
Step5: Display Checkbox
Output:
Output Explanation:
4.Radio Button:
The Radio button widget is used to select a single item from a predefined list of options.
Example:
import tkinter as tk
T = tk.Tk()
T.geometry("300x150")
l1 = tk.Label(text = "Select your gender")
l1.pack()
Var1 = tk.IntVar()
def selection():
selection = "You selected the option " + str(Var1.get())
l2.config(text = selection)
R1 = tk.Radiobutton(T, text="Male", variable=Var1, value=1,
command=selection)
R1.pack( anchor = 'w' )
R2 = tk.Radiobutton(T, text="Female", variable=Var1, value=2,
command=selection)
R2.pack( anchor = 'w' )
R3 = tk.Radiobutton(T, text="Others", variable=Var1, value=3,
command=selection)
R3.pack( anchor = 'w')
l2 = tk.Label(T)
l2.pack()
T.mainloop()
import tkinter as tk
T = tk.Tk()T.geometry("300x150")
l1 = tk.Label(text = "Select your gender")
l1.pack()
Var1 = tk.IntVar()
def selection():
selection = "You selected the option " + str(Var1.get())
l2.config(text = selection)
R1 = tk.Radiobutton(T, text="Male", variable=Var1, value=1,
command=selection)
R1.pack( anchor = 'w' )
R2 = tk.Radiobutton(T, text="Female", variable=Var1, value=2,
command=selection)
R2.pack( anchor = 'w' )
R3 = tk.Radiobutton(T, text="Others", variable=Var1, value=3,
command=selection)
R3.pack( anchor = 'w')
l2 = tk.Label(T)
l2.pack()
T.mainloop()
Super
ReplyDelete