0% found this document useful (0 votes)
43 views7 pages

Tkinter Widgets and Layouts Guide

The document provides an overview of Tkinter widgets, including basic widgets like Label, Button, and Entry, as well as container and special widgets. It also explains geometry managers (pack, grid, place) for layout management and discusses padding for spacing around widgets. Additionally, it includes example code snippets for creating various GUI elements such as buttons, checkboxes, and listboxes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views7 pages

Tkinter Widgets and Layouts Guide

The document provides an overview of Tkinter widgets, including basic widgets like Label, Button, and Entry, as well as container and special widgets. It also explains geometry managers (pack, grid, place) for layout management and discusses padding for spacing around widgets. Additionally, it includes example code snippets for creating various GUI elements such as buttons, checkboxes, and listboxes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Tkinter Widgets:

In Tkinter, a widget is any element of the GUI — like a button, label, text box,
etc.
They are the building blocks of a GUI application.
 Basic Widgets:
 Label – Displays text or images.
label = [Link](root, text="Hello Tkinter!")

 Button – A clickable button.


button = [Link](root, text="Click Me")

 Entry – A single-line text box for user input.


entry = [Link](root)

 Text – A multi-line text area.

text = [Link](root, height=5, width=30)

 Checkbutton – A checkbox for multiple choices.

check = [Link](root, text="I Agree")

 Radiobutton – Lets user pick one option from a group.

radio = [Link](root, text="Option 1")

 Listbox – Displays a list of selectable items.

listbox = [Link](root)

 Container Widgets:
 Frame – A container to group other widgets.

frame = [Link](root)

 LabelFrame – A labeled container for grouping widgets.

labelframe = [Link](root, text="Options")

 Special Widgets:
 Canvas – For drawing shapes, images, or graphics.
canvas = [Link](root, width=200, height=100)

 Scale – A slider to choose a value.

scale = [Link](root, from_=0, to=100,


orient="horizontal")

 Spinbox – A numeric up-down counter.

spin = [Link](root, from_=1, to=10)

 Menu – Creates menus (File, Edit, etc.).

menubar = [Link](root)

 Scrollbar – Adds scrolling ability to Text/Listbox/Canvas.

scrollbar = [Link](root)

 Message – Like Label, but for longer text with automatic wrapping.

msg = [Link](root, text="This is a longer


message.")

[Link] Managers in Tkinter

1. pack()

 Packs widgets relative to each other (top, bottom, left, right).

 Simple, but not for complex layouts.

import tkinter as tk

root = [Link]()

[Link]("pack() Example")

[Link](root, text="Top",
bg="lightblue").pack(side="top")

[Link](root, text="Bottom",
bg="lightgreen").pack(side="bottom")

[Link](root, text="Left",
bg="lightpink").pack(side="left")
[Link](root, text="Right",
bg="lightyellow").pack(side="right")

[Link]()

2. grid()

 Places widgets in a table-like (row, column) structure.

 More flexible for forms and structured layouts.

import tkinter as tk

root = [Link]()

[Link]("grid() Example")

[Link](root, text="Name:").grid(row=0, column=0)

[Link](root).grid(row=0, column=1)

[Link](root, text="Email:").grid(row=1, column=0)

[Link](root).grid(row=1, column=1)

[Link](root, text="Submit").grid(row=2,
column=1)

[Link]()

[Link]()

[Link](x=some_value, y=some_value)

 x → Horizontal position (pixels from left).

 y → Vertical position (pixels from top).

 relx, rely → Relative position (0.0 to 1.0, fraction of window


width/height).

 anchor → Which part of the widget is placed at the (x, y). (e.g., center,
n, s, e, w)
🔹 Padding in Tkinter

Padding = space around widgets.

 padx = horizontal padding (left-right space).


 pady = vertical padding (top-bottom space).

import tkinter as tk

root = [Link]()

[Link]("Padding Example")

[Link](root, text="Username").grid(row=0,
column=0, padx=10, pady=5)

[Link](root).grid(row=0, column=1, padx=10,


pady=5)

[Link](root, text="Password").grid(row=1,
column=0, padx=10, pady=5)

[Link](root, show="*").grid(row=1, column=1,


padx=10, pady=5)

[Link](root, text="Login").grid(row=2, column=1,


pady=10)

[Link]()

 Import Tkinter, Create Window and Set Title


import tkinter as tk

# create window
root = [Link]()
[Link]("My First Tkinter Window")

[Link]("300x200") # optional (width x


height)
[Link]()

 Two Buttons: Exit and Hello


import tkinter as tk
from tkinter import messagebox
def say_hello():
[Link]("Hello", "Hello,
Tkinter!")

root = [Link]()
[Link]("Buttons Example")

hello_btn = [Link](root, text="Hello",


command=say_hello)
hello_btn.pack(pady=10)

exit_btn = [Link](root, text="Exit",


command=[Link])
exit_btn.pack(pady=10)

[Link]()

 Checkbutton Widget
import tkinter as tk

root = [Link]()
[Link]("Checkbutton Example")

var1 = [Link]()

check1 = [Link](root, text="I Agree",


variable=var1)
[Link]()

[Link]()

 Three Single Line Text-Boxes


import tkinter as tk

root = [Link]()
[Link]("Text Entry Example")

entry1 = [Link](root)
[Link](pady=5)
entry2 = [Link](root)
[Link](pady=5)

entry3 = [Link](root)
[Link](pady=5)

[Link]()

 Radio Buttons
import tkinter as tk

root = [Link]()
[Link]("Radio Button Example")

choice = [Link](value="Option 1")

r1 = [Link](root, text="Option 1",


variable=choice, value="Option 1")
[Link]()

r2 = [Link](root, text="Option 2",


variable=choice, value="Option 2")
[Link]()

r3 = [Link](root, text="Option 3",


variable=choice, value="Option 3")
[Link]()

[Link]()

 Listbox Widget
import tkinter as tk
root = [Link]()
[Link]("Listbox Example")

listbox = [Link](root)
[Link](pady=10)
items = ["Python", "Java", "C++", "JavaScript",
"PHP"]
for item in items:
[Link]([Link], item)

[Link]()

You might also like