0% found this document useful (0 votes)
31 views10 pages

Tkinter Programs

The document contains 3 code examples that create GUI interfaces using Tkinter in Python. The first example uses the pack layout manager to place 15 widgets, the second uses grid, and the third uses place. Each example defines a function to handle a button click, creates a main window, and adds labeled widgets like entries, buttons, checkboxes before starting the main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Tkinter Programs

The document contains 3 code examples that create GUI interfaces using Tkinter in Python. The first example uses the pack layout manager to place 15 widgets, the second uses grid, and the third uses place. Each example defines a function to handle a button click, creates a main window, and adds labeled widgets like entries, buttons, checkboxes before starting the main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1) import tkinter as tk

def on_button_click():

label.config(text="Hello, " + entry.get())

# Create the main window

root = tk.Tk()

root.title("Pack Example")

# Create a label

label = tk.Label(root, text="Enter your name:")

label.pack(pady=10) # pady adds some vertical padding

# Create an entry widget

entry = tk.Entry(root)

entry.pack(pady=10)

# Create a button

button = tk.Button(root, text="Say Hello", command=on_button_click)

button.pack()

# Start the main event loop

root.mainloop()

2) import tkinter as tk

def on_button_click():

label.config(text="Hello, " + entry.get())

# Create the main window

root = tk.Tk()

root.title("Pack Example with 15 Widgets")

# Widget 1: Label
label = tk.Label(root, text="Enter your name:")

label.pack(pady=5)

# Widget 2: Entry

entry = tk.Entry(root)

entry.pack(pady=5)

# Widget 3: Button

button = tk.Button(root, text="Say Hello", command=on_button_click)

button.pack(pady=5)

# Widget 4: Checkbutton

check_var = tk.IntVar()

checkbutton = tk.Checkbutton(root, text="Check me", variable=check_var)

checkbutton.pack(pady=5)

# Widget 5: Radiobuttons

radio_var = tk.StringVar()

radio_var.set("Option 1")

radio_button1 = tk.Radiobutton(root, text="Option 1", variable=radio_var,


value="Option 1")

radio_button2 = tk.Radiobutton(root, text="Option 2", variable=radio_var,


value="Option 2")

radio_button1.pack(pady=5)

radio_button2.pack(pady=5)

# Widget 6: Listbox

listbox = tk.Listbox(root)
for item in ["Item 1", "Item 2", "Item 3"]:

listbox.insert(tk.END, item)

listbox.pack(pady=5)

# Widget 7: Scale

scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL)

scale.pack(pady=5)

# Widget 8: Text

text = tk.Text(root, height=4, width=30)

text.pack(pady=5)

# Widget 9: Message

message = tk.Message(root, text="This is a Message widget.")

message.pack(pady=5)

# Widget 10: Spinbox

spinbox = tk.Spinbox(root, from_=0, to=10)

spinbox.pack(pady=5)

# Widget 11: Progressbar

progressbar = tk.Progressbar(root, orient=tk.HORIZONTAL, length=100,


mode='indeterminate')

progressbar.pack(pady=5)

# Widget 12: Canvas


canvas = tk.Canvas(root, width=200, height=100, bg='lightgray')

canvas.pack(pady=5)

# Widget 13: Menu

menu_bar = tk.Menu(root)

root.config(menu=menu_bar)

file_menu = tk.Menu(menu_bar, tearoff=0)

file_menu.add_command(label="New")

file_menu.add_command(label="Open")

file_menu.add_separator()

file_menu.add_command(label="Exit", command=root.destroy)

menu_bar.add_cascade(label="File", menu=file_menu)

# Widget 14: Frame

frame = tk.Frame(root, borderwidth=2, relief="ridge")

frame.pack(pady=5)

# Widget 15: LabelFrame

label_frame = tk.LabelFrame(root, text="Label Frame", padx=5, pady=5)

label_frame.pack(pady=5)

# Start the main event loop

root.mainloop()

3) import tkinter as tk

def on_button_click():

label.config(text="Hello, " + entry.get())


# Create the main window

root = tk.Tk()

root.title("Grid Example with 15 Widgets")

# Widget 1: Label

label = tk.Label(root, text="Enter your name:")

label.grid(row=0, column=0, pady=5, sticky=tk.W)

# Widget 2: Entry

entry = tk.Entry(root)

entry.grid(row=0, column=1, pady=5)

# Widget 3: Button

button = tk.Button(root, text="Say Hello", command=on_button_click)

button.grid(row=0, column=2, pady=5)

# Widget 4: Checkbutton

check_var = tk.IntVar()

checkbutton = tk.Checkbutton(root, text="Check me", variable=check_var)

checkbutton.grid(row=1, column=0, pady=5, sticky=tk.W)

# Widget 5: Radiobuttons

radio_var = tk.StringVar()

radio_var.set("Option 1")

radio_button1 = tk.Radiobutton(root, text="Option 1", variable=radio_var,


value="Option 1")
radio_button2 = tk.Radiobutton(root, text="Option 2", variable=radio_var,
value="Option 2")

radio_button1.grid(row=1, column=1, pady=5, sticky=tk.W)

radio_button2.grid(row=1, column=2, pady=5, sticky=tk.W)

# Widget 6: Listbox

listbox = tk.Listbox(root)

for item in ["Item 1", "Item 2", "Item 3"]:

listbox.insert(tk.END, item)

listbox.grid(row=2, column=0, columnspan=3, pady=5)

# Widget 7: Scale

scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL)

scale.grid(row=3, column=0, columnspan=3, pady=5)

# Widget 8: Text

text = tk.Text(root, height=4, width=30)

text.grid(row=4, column=0, columnspan=3, pady=5)

# Widget 9: Message

message = tk.Message(root, text="This is a Message widget.")

message.grid(row=5, column=0, columnspan=3, pady=5)

# Widget 10: Spinbox

spinbox = tk.Spinbox(root, from_=0, to=10)

spinbox.grid(row=6, column=0, pady=5, sticky=tk.W)


# Widget 11: Progressbar

progressbar = tk.Progressbar(root, orient=tk.HORIZONTAL, length=100,


mode='indeterminate')

progressbar.grid(row=6, column=1, columnspan=2, pady=5, sticky=tk.W)

# Widget 12: Canvas

canvas = tk.Canvas(root, width=200, height=100, bg='lightgray')

canvas.grid(row=7, column=0, columnspan=3, pady=5)

# Widget 13: Menu

menu_bar = tk.Menu(root)

root.config(menu=menu_bar)

file_menu = tk.Menu(menu_bar, tearoff=0)

file_menu.add_command(label="New")

file_menu.add_command(label="Open")

file_menu.add_separator()

file_menu.add_command(label="Exit", command=root.destroy)

menu_bar.add_cascade(label="File", menu=file_menu)

# Widget 14: Frame

frame = tk.Frame

import tkinter as tk

def on_button_click():

label.config(text="Hello, " + entry.get())

# Create the main window


root = tk.Tk()

root.title("Place Example with 15 Widgets")

# Widget 1: Label

label = tk.Label(root, text="Enter your name:")

label.place(x=10, y=10)

# Widget 2: Entry

entry = tk.Entry(root)

entry.place(x=130, y=10)

# Widget 3: Button

button = tk.Button(root, text="Say Hello", command=on_button_click)

button.place(x=250, y=10)

# Widget 4: Checkbutton

check_var = tk.IntVar()

checkbutton = tk.Checkbutton(root, text="Check me", variable=check_var)

checkbutton.place(x=10, y=40)

# Widget 5: Radiobuttons

radio_var = tk.StringVar()

radio_var.set("Option 1")

radio_button1 = tk.Radiobutton(root, text="Option 1", variable=radio_var,


value="Option 1")

radio_button2 = tk.Radiobutton(root, text="Option 2", variable=radio_var,


value="Option 2")

radio_button1.place(x=130, y=40)
radio_button2.place(x=250, y=40)

# Widget 6: Listbox

listbox = tk.Listbox(root)

for item in ["Item 1", "Item 2", "Item 3"]:

listbox.insert(tk.END, item)

listbox.place(x=10, y=70)

# Widget 7: Scale

scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL)

scale.place(x=130, y=70)

# Widget 8: Text

text = tk.Text(root, height=4, width=30)

text.place(x=250, y=70)

# Widget 9: Message

message = tk.Message(root, text="This is a Message widget.")

message.place(x=10, y=150)

# Widget 10: Spinbox

spinbox = tk.Spinbox(root, from_=0, to=10)

spinbox.place(x=130, y=150)

# Widget 11: Progressbar

progressbar = tk.Progressbar(root, orient=tk.HORIZONTAL, length=100,


mode='indeterminate')
progressbar.place(x=250, y=150)

# Widget 12: Canvas

canvas = tk.Canvas(root, width=200, height=100, bg='lightgray')

canvas.place(x=10, y=180)

# Widget 13: Menu (not placed with `place`)

menu_bar = tk.Menu(root)

root.config(menu=menu_bar)

file_menu = tk.Menu(menu_bar, tearoff=0)

file_menu.add_command(label="New")

file_menu.add_command(label="Open")

file_menu.add_separator()

file_menu.add_command(label="Exit", command=root.destroy)

menu_bar.add_cascade(label="File", menu=file_menu)

# Widget 14: Frame (not placed with `place`)

frame = tk.Frame(root, borderwidth=2, relief="ridge")

frame.pack(pady=5)

# Widget 15: LabelFrame (not placed with `place`)

label_frame = tk.LabelFrame(root, text="Label Frame", padx=5, pady=5)

label_frame.pack(pady=5)

# Start the main event loop

root.mainloop()

You might also like