0% found this document useful (0 votes)
8 views5 pages

Practicak 28

The document contains multiple code snippets demonstrating the use of Tkinter for creating GUI applications in Python. It includes examples of creating buttons, labels, checkboxes, radio buttons, and menu bars with various functionalities. Each snippet is designed to showcase different aspects of Tkinter's capabilities for building interactive applications.
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)
8 views5 pages

Practicak 28

The document contains multiple code snippets demonstrating the use of Tkinter for creating GUI applications in Python. It includes examples of creating buttons, labels, checkboxes, radio buttons, and menu bars with various functionalities. Each snippet is designed to showcase different aspects of Tkinter's capabilities for building interactive applications.
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/ 5

1]

import tkinter as tk

def on_bu on_click():


label.config(text="Bu on Clicked!")

# Create the main window


root = tk.Tk()
root. tle("Tkinter GUI Example")
root.geometry("300x200")

# Create a label
label = tk.Label(root, text="Hello, Tkinter!", font=("Arial", 14))
label.pack(pady=10)

# Create a bu on
bu on = tk.Bu on(root, text="Click me", font=("Arial", 12), command=on_bu on_click)
bu on.pack(pady=10)

# Create a checkbu on
check_var = tk.IntVar()
check_bu on = tk.Checkbu on(root, text="Check Me", font=("Arial", 12), variable=check_var)
check_bu on.pack(pady=10)

# Run the Tkinter event loop


root.mainloop()
2]
# Impor ng Tkinter module
from tkinter import Tk, StringVar, Radiobu on, X

# Crea ng master Tkinter Window


master = Tk()
master.geometry("175x175")

# Tkinter string variable to store selected value


v = StringVar(master, "1")

# Dic onary to create mul ple radio bu ons


values = {
"RadioBu on 1": "1",
"RadioBu on 2": "2",
"RadioBu on 3": "3",
"RadioBu on 4": "4",
"RadioBu on 5": "5"
}

# Loop to create mul ple Radiobu ons


for (text, value) in values.items():
Radiobu on(master, text=text, variable=v, value=value, indicator=0,
background="light blue").pack(fill=X, ipady=5)

# Run the Tkinter event loop


master.mainloop()
4]
import tkinter as tk
from tkinter import messagebox

# Func on for menu ac ons


def new_file():
messagebox.showinfo("New File", "New file created!")

def open_file():
messagebox.showinfo("Open File", "Opening file...")

def exit_app():
root.destroy()

def cut_text():
messagebox.showinfo("Cut", "Cut ac on selected")

def copy_text():
messagebox.showinfo("Copy", "Copy ac on selected")

def paste_text():
messagebox.showinfo("Paste", "Paste ac on selected")

def about_app():
messagebox.showinfo("About", "Tkinter Menu Example\nVersion 1.0")

# Create main window


root = tk.Tk()
root. tle("Tkinter Menu Example")
root.geometry("400x300")

# Create Menu Bar


menu_bar = tk.Menu(root)

# File Menu
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New", command=new_file) # Fixed
file_menu.add_command(label="Open", command=open_file) # Fixed
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_app) # Fixed

# Edit Menu
edit_menu = tk.Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Cut", command=cut_text) # Fixed
edit_menu.add_command(label="Copy", command=copy_text) # Fixed
edit_menu.add_command(label="Paste", command=paste_text) # Fixed

# Help Menu
help_menu = tk.Menu(menu_bar, tearoff=0)
help_menu.add_command(label="About", command=about_app) # Fixed
# Add Menus to Menu Bar
menu_bar.add_cascade(label="File", menu=file_menu)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
menu_bar.add_cascade(label="Help", menu=help_menu)

# A ach Menu Bar to Window


root.config(menu=menu_bar)

# Run the applica on


root.mainloop()

You might also like