Thanks to visit codestin.com
Credit goes to www.scribd.com

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

First Simple Projects

project in python for console

Uploaded by

arpitapandit174
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)
11 views5 pages

First Simple Projects

project in python for console

Uploaded by

arpitapandit174
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

First simple Projects

import tkinter as tk

from tkinter import messagebox

# Functions

def add_task():

task = entry.get()

if task != "":

listbox.insert(tk.END, task)

entry.delete(0, tk.END)

else:

messagebox.showwarning("Warning", "Task cannot be empty!")

def remove_task():

try:

selected = listbox.curselection()[0]

listbox.delete(selected)

except IndexError:

messagebox.showwarning("Warning", "Please select a task to remove!")

def update_task():

try:

selected = listbox.curselection()[0]

new_task = entry.get()

if new_task != "":

listbox.delete(selected)

listbox.insert(selected, new_task)
entry.delete(0, tk.END)

else:

messagebox.showwarning("Warning", "Updated task cannot be empty!")

except IndexError:

messagebox.showwarning("Warning", "Please select a task to update!")

def exit_app():

root.destroy()

# GUI Setup

root = tk.Tk()

root.title(" To-Do List App")

root.geometry("400x400")

root.config(bg="#f5f5f5")

# Input field

entry = tk.Entry(root, width=30, font=("Arial", 14))

entry.pack(pady=10)

# Buttons

btn_frame = tk.Frame(root, bg="#f5f5f5")

btn_frame.pack(pady=5)

tk.Button(btn_frame, text="Add Task", width=10, command=add_task).grid(row=0,


column=0, padx=5)

tk.Button(btn_frame, text="Remove Task", width=10,


command=remove_task).grid(row=0, column=1, padx=5)

tk.Button(btn_frame, text="Update Task", width=10,


command=update_task).grid(row=0, column=2, padx=5)
tk.Button(root, text="Exit", width=10, command=exit_app).pack(pady=5)

# Task List

listbox = tk.Listbox(root, width=40, height=10, font=("Arial", 12))

listbox.pack(pady=10)

root.mainloop()
Second Simple Project
import tkinter as tk

# Function to update expression in the text entry

def press(key):

entry_var.set(entry_var.get() + str(key))

# Function to evaluate the final expression

def equalpress():

try:

result = str(eval(entry_var.get()))

entry_var.set(result)

except:

entry_var.set("Error")

# Function to clear the entry

def clear():

entry_var.set("")

# Create main window

root = tk.Tk()

root.title("Basic Calculator")

root.geometry("300x400")

# Entry widget for display

entry_var = tk.StringVar()

entry = tk.Entry(root, textvariable=entry_var, font=("Arial", 20), bd=10, relief="sunken",


justify="right")
entry.grid(row=0, column=0, columnspan=4, ipadx=8, ipady=15, pady=10)

# Button layout

buttons = [

('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),

('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),

('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),

('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),

# Create buttons dynamically

for (text, row, col) in buttons:

if text == "=":

btn = tk.Button(root, text=text, font=("Arial", 18), height=2, width=5,

command=equalpress, bg="lightgreen")

else:

btn = tk.Button(root, text=text, font=("Arial", 18), height=2, width=5,

command=lambda t=text: press(t))

btn.grid(row=row, column=col, padx=5, pady=5)

# Clear button

clear_btn = tk.Button(root, text="C", font=("Arial", 18), height=2, width=22,


bg="lightcoral", command=clear)

clear_btn.grid(row=5, column=0, columnspan=4, padx=5, pady=5)

# Run the app

root.mainloop()

You might also like