0% found this document useful (0 votes)
20 views4 pages

Tkinterexamples 1

The document contains examples of using different Tkinter widgets like buttons, labels, entries, checkbuttons, radiobuttons and listboxes in Python. It shows how to create the main window, add widgets, handle events and pack the widgets using grid or pack layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Tkinterexamples 1

The document contains examples of using different Tkinter widgets like buttons, labels, entries, checkbuttons, radiobuttons and listboxes in Python. It shows how to create the main window, add widgets, handle events and pack the widgets using grid or pack layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

mport tkinter as tk

def add():
num1 = float(num1_entry.get())
num2 = float(num2_entry.get())
result = num1 + num2
result_label.config(text=result)

# create the main window


root = tk.Tk()
root.title("Add Two Numbers")

# create the widgets


num1_label = tk.Label(root, text="Number 1:")
num1_entry = tk.Entry(root)
num2_label = tk.Label(root, text="Number 2:")
num2_entry = tk.Entry(root)
add_button = tk.Button(root, text="Add", command=add)
result_label = tk.Label(root, text="Result:")

# layout the widgets


num1_label.grid(row=0, column=0, sticky="e")
num1_entry.grid(row=0, column=1)
num2_label.grid(row=1, column=0, sticky="e")
num2_entry.grid(row=1, column=1)
add_button.grid(row=2, column=0, columnspan=2, pady=10)
result_label.grid(row=3, column=0, columnspan=2)

# run the main loop


root.mainloop()

example2

from tkinter import *


from functools import partial

def validateLogin(username, password):


print("username entered :", username.get())
print("password entered :", password.get())
return

# Window
tkWindow = Tk()
tkWindow.geometry('400x150')
tkWindow.title('Tkinter Login Form - pythonexamples.org')

# Username label and text entry box


usernameLabel = Label(tkWindow, text="User Name").grid(row=0, column=0)
username = StringVar()
usernameEntry = Entry(tkWindow, textvariable=username).grid(row=0, column=1)

# Password label and password entry box


passwordLabel = Label(tkWindow,text="Password").grid(row=1, column=0)
password = StringVar()
passwordEntry = Entry(tkWindow, textvariable=password, show='*').grid(row=1,
column=1)
validateLogin = partial(validateLogin, username, password)

# Login button
loginButton = Button(tkWindow, text="Login", command=validateLogin).grid(row=4,
column=0)

tkWindow.mainloop()

example 3

import tkinter as tk

def set_values():
checkbutton_1_var.set(0)
checkbutton_2_var.set(1)
checkbutton_3_var.set(1)

# Create the main window


window = tk.Tk()
window.geometry("300x200")
window.title("PythonExamples.org")

# Variables for Checkbuttons


checkbutton_1_var = tk.IntVar()
checkbutton_2_var = tk.IntVar()
checkbutton_3_var = tk.IntVar()

# Create Checkbuttons in a loop


checkbutton_1 = tk.Checkbutton(window, text="Option 1", variable=checkbutton_1_var)
checkbutton_2 = tk.Checkbutton(window, text="Option 2", variable=checkbutton_2_var)
checkbutton_3 = tk.Checkbutton(window, text="Option 3", variable=checkbutton_3_var)

checkbutton_1.pack()
checkbutton_2.pack()
checkbutton_3.pack()

example 4
import tkinter as tk
from tkinter import messagebox

# Create the main window


window = tk.Tk()

# Show an error message box


messagebox.showerror("Error", "This is an error message.")

# Run the application


window.mainloop()
example

import tkinter as tk

# Create the main window


window = tk.Tk()
window.geometry("300x200")
window.title("PythonExamples.org")

def on_radio_change():
selected_option = radio_var.get()
print(f"Selected option : {selected_option}")

# Create a variable to hold the selected option


radio_var = tk.StringVar()

# Create Radiobuttons
radio_button1 = tk.Radiobutton(window, text="Option 1", variable=radio_var,
value="Option 1", command=on_radio_change)
radio_button2 = tk.Radiobutton(window, text="Option 2", variable=radio_var,
value="Option 2", command=on_radio_change)
radio_button3 = tk.Radiobutton(window, text="Option 3", variable=radio_var,
value="Option 3", command=on_radio_change)

# Pack the Radiobuttons


radio_button1.pack()
radio_button2.pack()
radio_button3.pack()

# Start the Tkinter event loop


window.mainloop()

example

import tkinter as tk

def on_selection(event):
selected_indices = listbox.curselection()
for index in selected_indices:
print("Selected item:", listbox.get(index))

# Create the main window


window = tk.Tk()
window.geometry("300x200")
window.title("PythonExamples.org")

listbox = tk.Listbox(window)
listbox.pack()

# Inserting items into the listbox


listbox.insert(tk.END, "Item 1")
listbox.insert(tk.END, "Item 2")
listbox.insert(tk.END, "Item 3")
listbox.insert(tk.END, "Item 4")
# Binding the selection event
listbox.bind("<<ListboxSelect>>", on_selection)

window.mainloop()

button = tk.Button(window, text="Set values", command=set_values)


button.pack()

# Start the Tkinter event loop


window.mainloop()

You might also like