GUI Programming with Tkinter
Basic Window Creation
This example demonstrates the creation of a basic window using Tkinter. It includes setting the
window title and size.
from tkinter import *
# Initialize the main window
root = Tk()
root.title("Basic Window")
root.geometry("400x300") # Set the size of the window
# Main event loop to keep the window open
root.mainloop()
Output: A window titled 'Basic Window' opens with dimensions 400x300.
Adding a Label
This program shows how to add a text label to a Tkinter window.
from tkinter import *
root = Tk()
root.title("Label Example")
# Adding a label widget
label = Label(root, text="Welcome to Tkinter GUI Programming!", font=("Arial", 16))
label.pack(pady=20) # Place the label with padding
root.mainloop()
Output: A window with the text 'Welcome to Tkinter GUI Programming!' displayed.
Button with Click Event
This example demonstrates how to add a button that triggers an action when clicked.
from tkinter import *
def on_button_click():
print("Button was clicked!")
root = Tk()
root.title("Button Example")
# Adding a button widget
button = Button(root, text="Click Me!", command=on_button_click, bg="blue", fg="white")
button.pack(pady=20)
root.mainloop()
Output: A window with a button that prints 'Button was clicked!' to the console when clicked.
Input Field and Display Output
This example demonstrates how to use an input field to accept user input and display it on the
window.
from tkinter import *
def display_text():
entered_text = entry.get() # Get text from entry widget
label.config(text=f"Hello, {entered_text}!") # Update label text
root = Tk()
root.title("Input Example")
# Adding an entry widget
entry = Entry(root, width=30)
entry.pack(pady=10)
# Adding a button
button = Button(root, text="Submit", command=display_text)
button.pack(pady=5)
# Adding a label to display output
label = Label(root, text="")
label.pack(pady=10)
root.mainloop()
Output: A window with an input field, button, and a label displaying the input text.
Canvas for Drawing Shapes
This example shows how to use the Canvas widget to draw shapes like rectangles and ovals.
from tkinter import *
root = Tk()
root.title("Canvas Example")
# Adding a canvas widget
canvas = Canvas(root, width=300, height=200, bg="lightgray")
canvas.pack(pady=20)
# Drawing shapes
canvas.create_rectangle(50, 50, 150, 100, fill="blue")
canvas.create_oval(200, 50, 300, 150, fill="red")
root.mainloop()
Output: A window with a canvas displaying a blue rectangle and a red oval.
Menu Bar Example
This program demonstrates how to add a menu bar with dropdown options in a Tkinter application.
from tkinter import *
root = Tk()
root.title("Menu Example")
# Creating a menu bar
menu_bar = Menu(root)
root.config(menu=menu_bar)
# Adding a File menu
file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_command(label="Exit", command=root.quit)
root.mainloop()
Output: A window with a menu bar containing 'File' options like New, Open, and Exit.
MessageBox Example
This example shows how to use the messagebox module to display an information dialog.
from tkinter import *
from tkinter import messagebox
def show_message():
messagebox.showinfo("Information", "This is a message box example!")
root = Tk()
root.title("MessageBox Example")
# Adding a button to trigger the message box
button = Button(root, text="Show Message", command=show_message)
button.pack(pady=20)
root.mainloop()
Output: A window with a button that shows an information dialog when clicked.
Final Project: Basic Calculator
This is a simple calculator application that performs addition.
from tkinter import *
def calculate():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 + num2
label_result.config(text=f"Result: {result}")
root = Tk()
root.title("Basic Calculator")
# Input fields
entry1 = Entry(root, width=10)
entry1.pack(pady=5)
entry2 = Entry(root, width=10)
entry2.pack(pady=5)
# Button to perform calculation
button = Button(root, text="Add", command=calculate)
button.pack(pady=10)
# Label to display the result
label_result = Label(root, text="Result: ")
label_result.pack(pady=10)
root.mainloop()
Output: A window that performs addition of two input numbers and displays the result.