0% found this document useful (0 votes)
6 views

Python Assignments

Uploaded by

121sakshi4006
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)
6 views

Python Assignments

Uploaded by

121sakshi4006
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
You are on page 1/ 5

Rutuja T. Patil Roll No.

: 41
Python Assignment no. 01

Program:
import tkinter as tk
from tkinter import messagebox
from tkinter.filedialog import askopenfilename, asksaveasfilename
current_file = None
def new_file():
global current_file
text.delete(1.0, tk.END)
current_file = None
def open_file():
global current_file
filetypes = (("Text Files", "*.txt"), ("All Files", "*.*"))
filename = askopenfilename(defaultextension=".txt", filetypes=filetypes)
if filename:
with open(filename, "r") as file:
text.delete(1.0, tk.END)
text.insert(1.0, file.read())
current_file = filename
def save_file():
global current_file
if current_file:
with open(current_file, "w") as file:
file.write(text.get(1.0, tk.END))
else:
save_file_as()
def save_file_as():
global current_file
filetypes = (("Text Files", "*.txt"), ("All Files", "*.*"))
filename = asksaveasfilename(defaultextension=".txt", filetypes=filetypes)
if filename:
with open(filename, "w") as file:
file.write(text.get(1.0, tk.END))
current_file = filename
def quit_app():
if messagebox.askyesno("Quit", "Are you sure you want to quit?"):
root.destroy()
root = tk.Tk()
root.title("Notepad")
menu = tk.Menu(root)
file_menu = tk.Menu(menu, tearoff=False)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open...", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_command(label="Save As...", command=save_file_as)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit_app)
edit_menu = tk.Menu(menu, tearoff=False)
edit_menu.add_command(label="Undo")
edit_menu.add_separator()
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Paste")
edit_menu.add_command(label="Delete")
edit_menu.add_separator()
edit_menu.add_command(label="Find...")
edit_menu.add_command(label="Find Next")
edit_menu.add_command(label="Replace...")
edit_menu.add_command(label="Go To...")
edit_menu.add_separator()
edit_menu.add_command(label="Select All")
edit_menu.add_command(label="Time/Date")
format_menu = tk.Menu(menu, tearoff=False)
format_menu.add_command(label="Word Wrap")
format_menu.add_command(label="Font...")
view_menu = tk.Menu(menu, tearoff=False)
view_menu.add_command(label="Status Bar")
help_menu = tk.Menu(menu, tearoff=False)
help_menu.add_command(label="View Help")
help_menu.add_command(label="About Notepad")
menu.add_cascade(label="File", menu=file_menu)
menu.add_cascade(label="Edit", menu=edit_menu)
menu.add_cascade(label="Format", menu=format_menu)
menu.add_cascade(label="View", menu=view_menu)
menu.add_cascade(label="Help", menu=help_menu)
text = tk.Text(root)
text.pack(expand=True, fill=tk.BOTH)
root.config(menu=menu)
root.mainloop()
Output:
Rutuja T. Patil Roll No.: 41
Python Assignment no. 02

Program:

import tkinter as tk
import csv

class RegistrationForm:
def __init__(self, root):
self.root = root
self.root.title("Registration Form")
tk.Label(self.root, text="Name").grid(row=0, column=0)
self.name_entry = tk.Entry(self.root)
self.name_entry.grid(row=0, column=1)
tk.Label(self.root, text="Email").grid(row=1, column=0)
self.email_entry = tk.Entry(self.root)
self.email_entry.grid(row=1, column=1)
tk.Label(self.root, text="Phone").grid(row=2, column=0)
self.phone_entry = tk.Entry(self.root)
self.phone_entry.grid(row=2, column=1
tk.Label(self.root, text="Birthdate").grid(row=3, column=0)
self.birthdate_entry = tk.Entry(self.root)
self.birthdate_entry.grid(row=3, column=1)
tk.Label(self.root, text="City").grid(row=4, column=0)
self.city_entry = tk.Entry(self.root)
self.city_entry.grid(row=4, column=1)
tk.Button(self.root, text="Submit", command=self.submit).grid(row=5, column=0)
def submit(self):
name = self.name_entry.get()
email = self.email_entry.get()
phone = self.phone_entry.get()
birthdate = self.birthdate_entry.get()
city = self.city_entry.get()
with open("registrations.csv", "a", newline="") as file:
writer = csv.writer(file)
writer.writerow([name, email, phone, birthdate, city])
self.name_entry.delete(0, "end")
self.email_entry.delete(0, "end")
self.phone_entry.delete(0, "end")
self.birthdate_entry.delete(0, "end")
self.city_entry.delete(0, "end")
root = tk.Tk()
registration_form = RegistrationForm(root)
root.mainloop()
Output:

You might also like