Python Tkinter Module
Tkinter is the standard GUI (Graphical User Interface) library for Python. It provides a fast and easy way to create GUI applications.
Importing the Module
import tkinter as tk
Basic Window
import tkinter as tk
root = tk.Tk()
root.title("My App")
root.geometry("300x200")
# Start the event loop
root.mainloop()
Widgets
Label
label = tk.Label(root, text="Hello World")
label.pack()
Button
def on_click():
print("Button clicked!")
btn = tk.Button(root, text="Click Me", command=on_click)
btn.pack()
Entry (Input)
entry = tk.Entry(root)
entry.pack()
def get_input():
print(entry.get())
Layout Managers
pack(): Packs widgets in blocks before placing them in the parent widget.grid(): Places widgets in a 2D table.place(): Places widgets at a specific position.
# Grid Example
label1 = tk.Label(root, text="Name")
label1.grid(row=0, column=0)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1)