Organizing Layout And Widgets :
import tkinter
window = tkinter.Tk()
window.title("GUI")
top_frame = tkinter.Frame(window).pack()
bottom_frame = tkinter.Frame(window).pack(side="bottom")
btn1 = tkinter.Button(top_frame, text="Button1", fg="red").pack()
btn2 = tkinter.Button(top_frame, text="Button2", fg="green").pack()
btn3 = tkinter.Button(bottom_frame, text="Button3", fg="purple").pack(side="left")
btn4 = tkinter.Button(bottom_frame, text="Button4", fg="orange").pack(side="left")
window.mainloop()
Output :
Grid :
import tkinter
from tkinter import *
top = tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
tkinter.Checkbutton(
top,
text = "Machine Learning",
variable = CheckVar1,
onvalue = 1,
offvalue = 0
).grid(row=0, sticky=W)
tkinter.Checkbutton(
top,
text = "Deep Learning",
variable = CheckVar2,
onvalue = 0,
offvalue = 1
).grid(row=1, sticky=W)
top.mainloop()
Output :
Another example Grid :
import tkinter
window = tkinter.Tk()
window.title("GUI")
tkinter.Label(window, text="Username").grid(row=0)
tkinter.Entry(window).grid(row=0, column=1)
tkinter.Label(window, text="Password").grid(row=1)
tkinter.Entry(window).grid(row=1, column=1)
tkinter.Checkbutton(window, text="Keep Me Logged In").grid(columnspan=2)
window.mainloop()
Output :
Binding or Command Functions :
import tkinter
window = tkinter.Tk()
window.title("GUI")
def DataCamp_Tutorial():
tkinter.Label(window, text="GUI with Tkinter!").pack()
tkinter.Button(window, text="Click Me!", command=DataCamp_Tutorial).pack()
window.mainloop()
Output :
Mouse Clicking Event via the Bind Method :
import tkinter
window = tkinter.Tk()
window.title("GUI")
def left_click(event):
tkinter.Label(window, text="Left Click!").pack()
def middle_click(event):
tkinter.Label(window, text="Middle Click!").pack()
def right_click(event):
tkinter.Label(window, text="Right Click!").pack()
window.bind("<Button-1>", left_click)
window.bind("<Button-2>", middle_click)
window.bind("<Button-3>", right_click)
window.mainloop()
Output :
Alert Boxes :
import tkinter
import tkinter.messagebox
window = tkinter.Tk()
window.title("GUI")
tkinter.messagebox.showinfo("Alert Message", "This is just a alert message!")
response = tkinter.messagebox.askquestion("Tricky Question", "Do you love Deep Learning?")
if response == "yes":
tkinter.Label(window, text="Yes, offcourse I love Deep Learning!").pack()
else:
tkinter.Label(window, text="No, I don't love Deep Learning!").pack()
window.mainloop()
Output :
Rendering Images :
import tkinter
window = tkinter.Tk()
window.title("GUI")
icon = tkinter.PhotoImage(file="CNN.png")
label = tkinter.Label(window, image=icon)
label.pack()
window.mainloop()
Output :
Creating a Calculator with Tkinter :
from tkinter import *
window = Tk()
window.geometry("312x324")
window.resizable(0, 0)
window.title("Calculator")
def btn_click(item):
global expression
expression += str(item)
input_text.set(expression)
def btn_clear():
global expression
expression = ""
input_text.set("")
def btn_equal():
global expression
result = str(eval(expression))
input_text.set(result)
expression = ""
expression = ""
input_text = StringVar()
input_frame = Frame(window, width=312, height=50, bd=0, highlightbackground="black", highlightthickness=1)
input_frame.pack(side=TOP)
input_field = Entry(input_frame, font=('arial', 18, 'bold'), textvariable=input_text, width=50, bg="#eee", bd=0, justify=RIGHT)
input_field.grid(row=0, column=0)
input_field.pack(ipady=10)
btns_frame = Frame(window, width=312, height=272.5, bg="grey")
btns_frame.pack()
Button(btns_frame, text="C", width=32, height=3, command=btn_clear).grid(row=0, column=0, columnspan=3)
Button(btns_frame, text="/", width=10, height=3, command=lambda: btn_click("/")).grid(row=0, column=3)
window.mainloop()
Output :