Python Projects

Happy New Year

 
                

Source Code

import turtle screen = turtle.Screen() screen.setup(width=1.0, height=1.0) turtle.bgcolor('white') tr = turtle.Turtle() tr.speed("fastest") tr.up() # left decoration for i in range(10): for i in range(2): tr.pensize(7) tr.goto(-500, 0) tr.down() tr.color("purple") tr.forward(100) tr.circle(5, steps=4) tr.right(60) tr.color("violet") tr.forward(50) tr.right(120) tr.right(30) tr.up() # right decoration for i in range(10): for i in range(2): tr.pensize(7) tr.goto(470, 0) tr.down() tr.color("red") tr.forward(100) tr.circle(5, steps=4) tr.right(60) tr.color("crimson") tr.forward(50) tr.right(120) tr.right(30) tr.up() # top decoration for i in range(10): for i in range(2): tr.pensize(7) tr.goto(20, 265) tr.down() tr.color("forest green") tr.forward(100) tr.circle(5, steps=4) tr.right(60) tr.color("lime") tr.forward(50) tr.right(120) tr.right(30) tr.up() # bottom decoration for i in range(10): for i in range(2): tr.pensize(7) tr.goto(20, -220) tr.down() tr.color("dark turquoise") tr.forward(100) tr.circle(5, steps=4) tr.right(60) tr.color("cyan") tr.forward(50) tr.right(120) tr.right(30) turtle.up() turtle.color('red') turtle.goto(-320, 40) turtle.write("Happy ", font=(None, 60)) turtle.goto(-60, 40) turtle.color('deep pink') turtle.write("New", font=(None, 60)) turtle.goto(145, 40) turtle.color('blue') turtle.write("Year", font=(None, 60)) turtle.goto(-74, -60) turtle.color('green') turtle.write("2023!", font=(None, 60)) turtle.don

Calculator

  
                

Source Code

import tkinter as tk LARGE_FONT_STYLE = ("Arial", 40, "bold") SMALL_FONT_STYLE = ("Arial", 16) DIGITS_FONT_STYLE = ("Arial", 24, "bold") DEFAULT_FONT_STYLE = ("Arial", 20) OFF_WHITE = "#F8FAFF" WHITE = "#FFFFFF" LIGHT_BLUE = "#CCEDFF" LIGHT_GRAY = "#F5F5F5" LABEL_COLOR = "#25265E" class Calculator: def __init__(self): self.window = tk.Tk() self.window.geometry("375x667") self.window.resizable(0, 0) self.window.title("Calculator") self.total_expression = "" self.current_expression = "" self.display_frame = self.create_display_frame() self.total_label, self.label = self.create_display_labels() self.digits = { 7: (1, 1), 8: (1, 2), 9: (1, 3), 4: (2, 1), 5: (2, 2), 6: (2, 3), 1: (3, 1), 2: (3, 2), 3: (3, 3), 0: (4, 2), '.': (4, 1) } self.operations = {"/": "\u00F7", "*": "\u00D7", "-": "-", "+": "+"} self.buttons_frame = self.create_buttons_frame() self.buttons_frame.rowconfigure(0, weight=1) for x in range(1, 5): self.buttons_frame.rowconfigure(x, weight=1) self.buttons_frame.columnconfigure(x, weight=1) self.create_digit_buttons() self.create_operator_buttons() self.create_special_buttons() self.bind_keys() def bind_keys(self): self.window.bind("", lambda event: self.evaluate()) for key in self.digits: self.window.bind(str(key), lambda event, digit=key: self.add_to_expression(digit)) for key in self.operations: self.window.bind(key, lambda event, operator=key: self.append_operator(operator)) def create_special_buttons(self): self.create_clear_button() self.create_equals_button() self.create_square_button() self.create_sqrt_button() def create_display_labels(self): total_label = tk.Label(self.display_frame, text=self.total_expression, anchor=tk.E, bg=LIGHT_GRAY, fg=LABEL_COLOR, padx=24, font=SMALL_FONT_STYLE) total_label.pack(expand=True, fill='both') label = tk.Label(self.display_frame, text=self.current_expression, anchor=tk.E, bg=LIGHT_GRAY, fg=LABEL_COLOR, padx=24, font=LARGE_FONT_STYLE) label.pack(expand=True, fill='both') return total_label, label def create_display_frame(self): frame = tk.Frame(self.window, height=221, bg=LIGHT_GRAY) frame.pack(expand=True, fill="both") return frame def add_to_expression(self, value): self.current_expression += str(value) self.update_label() def create_digit_buttons(self): for digit, grid_value in self.digits.items(): button = tk.Button(self.buttons_frame, text=str(digit), bg=WHITE, fg=LABEL_COLOR, font=DIGITS_FONT_STYLE, borderwidth=0, command=lambda x=digit: self.add_to_expression(x)) button.grid(row=grid_value[0], column=grid_value[1], sticky=tk.NSEW) def append_operator(self, operator): self.current_expression += operator self.total_expression += self.current_expression self.current_expression = "" self.update_total_label() self.update_label() def create_operator_buttons(self): i = 0 for operator, symbol in self.operations.items(): button = tk.Button(self.buttons_frame, text=symbol, bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,borderwidth=0, command=lambda x=operator: self.append_operator(x)) button.grid(row=i, column=4, sticky=tk.NSEW) i += 1 def clear(self): self.current_expression = "" self.total_expression = "" self.update_label() self.update_total_label() def create_clear_button(self): button = tk.Button(self.buttons_frame, text="C", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.clear) button.grid(row=0, column=1, sticky=tk.NSEW) def square(self): self.current_expression = str(eval(f"{self.current_expression}**2")) self.update_label() def create_square_button(self): button = tk.Button(self.buttons_frame, text="x\u00b2", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.square) button.grid(row=0, column=2, sticky=tk.NSEW) def sqrt(self): self.current_expression = str(eval(f"{self.current_expression}**0.5")) self.update_label() def create_sqrt_button(self): button = tk.Button(self.buttons_frame, text="\u221ax", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.sqrt) button.grid(row=0, column=3, sticky=tk.NSEW) def evaluate(self): self.total_expression += self.current_expression self.update_total_label() try: self.current_expression = str(eval(self.total_expression)) self.total_expression = "" except Exception as e: self.current_expression = "Error" finally: self.update_label() def create_equals_button(self): button = tk.Button(self.buttons_frame, text="=", bg=LIGHT_BLUE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.evaluate) button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW) def create_buttons_frame(self): frame = tk.Frame(self.window) frame.pack(expand=True, fill="both") return frame def update_total_label(self): expression = self.total_expression for operator, symbol in self.operations.items(): expression = expression.replace(operator, f' {symbol} ') self.total_label.config(text=expression) def update_label(self): self.label.config(text=self.current_expression[:11]) def run(self): self.window.mainloop() if __name__ == "__main__": calc = Calculator() calc.run()

Love Detector

 
                

Source Code

# Python Tkinter GUI based "LOVE CALCULATOR" # import tkinter from tkinter import * # import random module import random # Creating GUI window root = Tk() # Defining the container size, width=400, height=240 root.geometry('400x240') # Title of the container root.title('Love Calculator????') # Function to calculate love percentage # between the user ans partner def calculate_love(): # value will contain digits between 0-9 st = '0123456789' # result will be in double digits digit = 2 temp = "".join(random.sample(st, digit)) result.config(text=temp) # Heading on Top heading = Label(root, text='Love Calculator - How much is he/she into you') heading.pack() # Slot/input for the first name slot1 = Label(root, text="Enter Your Name:") slot1.pack() name1 = Entry(root, border=5) name1.pack() # Slot/input for the partner name slot2 = Label(root, text="Enter Your Partner Name:") slot2.pack() name2 = Entry(root, border=5) name2.pack() bt = Button(root, text="Calculate", height=1, width=7, command=calculate_love) bt.pack() # Text on result slot result = Label(root, text='Love Percentage between both of You:') result.pack() # Starting the GUI root.mainloop()

Happy New Year

 
                

Source Code

Happy New Year

 
                

Source Code

...
...