Unit 10

Source Code

def main():
    done = False
    print("Welcome to the Main Function")
    while not done:
        print("Menu")
        print("E1 - Example 1")
        choice = input("Choice: ")
        match choice:
            case "E1":
                example1()
            case "E2":
                print(factorial_iterative(5))
            case "E3":
                print(factorial_recursive(5))
            case "E4":
                print(fibonacci(5))
            case "Q":
                print("Quitting!")
                done = True
            case _:
                print("Invalid, try again!")

# define Example 1 Function
def example1():
    print("Recursive Example!")
    example1()

# define factorial iterative Function
def factorial_iterative(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

# define factorial recursive Function
def factorial_recursive(n):
    # Base case: if n is 0, return 1
    if n == 0:
        return 1
    # Recursive case: multiply n by the factorial of (n - 1)
    else:
        return n * factorial_recursive(n - 1)

# define fibonacci function
def fibonacci(n):
    # Base cases
    if n == 0:
        return 0
    elif n == 1:
        return 1
    # Recursive case
    else:
        return fibonacci(n-1) + fibonacci(n-2)

# do not remove!
main()

gui1.py

import tkinter as tk

# Create the main window
root = tk.Tk()
root.title("My First Tkinter Application")
root.geometry("400x300")  # Width x Height

# Add widgets here

# Start the main loop
root.mainloop()

gui2.py

import tkinter as tk

def greet():
    name = entry.get()
    label.config(text=f"Hello, {name}!")

root = tk.Tk()
root.title("Greeting App")

label = tk.Label(root, text="Enter your name:")
label.pack()

entry = tk.Entry(root)
entry.pack()

button = tk.Button(root, text="Greet", command=greet)
button.pack()

root.mainloop()

gui3.py

import tkinter as tk

root = tk.Tk()
root.title("Grid Layout")

tk.Label(root, text="First Name").grid(row=0, column=0)
tk.Label(root, text="Last Name").grid(row=1, column=0)

entry1 = tk.Entry(root)
entry2 = tk.Entry(root)

entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)

root.mainloop()

gui4.py

import tkinter as tk

def on_click():
    print("Button clicked!")

root = tk.Tk()
button = tk.Button(root, text="Click Me", command=on_click)
button.pack()

root.mainloop()

gui5.py

import tkinter as tk
from tkinter import messagebox

def about():
    messagebox.showinfo("About", "This is a GUI application.")

root = tk.Tk()
root.title("Menu Example")

menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="About", command=about)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

root.config(menu=menubar)
root.mainloop()

gui6.py

import tkinter as tk
from tkinter import messagebox

def show_choice():
    messagebox.showinfo("Selection", f"You selected: {choice.get()}")

root = tk.Tk()

choice = tk.StringVar()
tk.Radiobutton(root, text="Option A", variable=choice, value="A").pack()
tk.Radiobutton(root, text="Option B", variable=choice, value="B").pack()

button = tk.Button(root, text="Show Choice", command=show_choice)
button.pack()

root.mainloop()

todo.py

import tkinter as tk

def add_task():
    task = entry.get()
    if task:
        listbox.insert(tk.END, task)
        entry.delete(0, tk.END)

def delete_task():
    try:
        selected = listbox.curselection()
        listbox.delete(selected[0])
    except IndexError:
        pass

root = tk.Tk()
root.title("To-Do List")

entry = tk.Entry(root, width=40)
entry.pack()

add_button = tk.Button(root, text="Add Task", command=add_task)
add_button.pack()

listbox = tk.Listbox(root, width=50, height=10)
listbox.pack()

delete_button = tk.Button(root, text="Delete Task", command=delete_task)
delete_button.pack()

root.mainloop()

project2.py

import tkinter as tk
from tkinter import messagebox

# Global dictionary to store the order
myOrder = {}

# Create the main application window
root = tk.Tk()
root.title("Restaurant Ordering System")
root.geometry("400x400")

# Function to display restaurant information
def show_info():
    info_text = ("Restaurant Name: Kean Bytes\n"
                 "Location: 1000 Morris Ave, Union NJ 07083\n"
                 "Website: www.kean.edu")
    messagebox.showinfo("Restaurant Information", info_text)

# Function to add items to the order
def add_to_order(item, price):
    def confirm_order():
        try:
            quantity = int(quantity_entry.get())
            if quantity <= 0:
                raise ValueError
            if item in myOrder:
                myOrder[item] += quantity
            else:
                myOrder[item] = quantity
            messagebox.showinfo("Order Updated", f"Added {quantity} {item}(s) to your order.")
            quantity_window.destroy()
        except ValueError:
            messagebox.showwarning("Invalid Input", "Please enter a positive integer for quantity.")

    # Create a new window for entering quantity
    quantity_window = tk.Toplevel(root)
    quantity_window.title("Enter Quantity")
    quantity_window.geometry("250x100")

    quantity_label = tk.Label(quantity_window, text=f"Enter quantity for {item}:")
    quantity_label.pack(pady=5)

    quantity_entry = tk.Entry(quantity_window, width=5)
    quantity_entry.insert(0, "1")
    quantity_entry.pack(pady=5)

    confirm_button = tk.Button(quantity_window, text="Confirm", command=confirm_order)
    confirm_button.pack(pady=5)

# Function to display the current order
def display_order():
    if myOrder:
        order_text = "\n".join([f"{item}: {qty}" for item, qty in myOrder.items()])
        messagebox.showinfo("Your Order", order_text)
    else:
        messagebox.showinfo("Your Order", "No items ordered yet.")

# Function to place the order (coming soon)
def place_order():
    messagebox.showinfo("Place Order", "Coming Soon!")

# Function to modify the order (coming soon)
def modify_order():
    messagebox.showinfo("Modify Order", "Modify Order feature coming soon!")

# Function to clear the frame
def clear_frame():
    for widget in menu_frame.winfo_children():
        widget.destroy()

# Function to go back to the main menu
def show_main_menu():
    clear_frame()
    category_label = tk.Label(menu_frame, text="Main Menu", font=("Arial", 16))
    category_label.pack(pady=10)

    tk.Button(menu_frame, text="Restaurant Info", command=show_info).pack(pady=5)
    tk.Button(menu_frame, text="Appetizers", command=show_appetizers).pack(pady=5)
    tk.Button(menu_frame, text="Entrees", command=lambda: messagebox.showinfo("Entrees", "Coming Soon!")).pack(pady=5)
    tk.Button(menu_frame, text="Desserts", command=lambda: messagebox.showinfo("Desserts", "Coming Soon!")).pack(pady=5)
    tk.Button(menu_frame, text="Beverages", command=lambda: messagebox.showinfo("Beverages", "Coming Soon!")).pack(pady=5)

    # Main Menu buttons
    tk.Button(menu_frame, text="Display Order", command=display_order).pack(pady=5)
    tk.Button(menu_frame, text="Modify Order", command=modify_order).pack(pady=5)
    tk.Button(menu_frame, text="Place Order", command=place_order).pack(pady=5)

# Function to show appetizers
def show_appetizers():
    # Appetizers dictionary
    appetizers = {
        "Garlic Bread": 5.99,
        "Mozzarella Sticks": 7.99,
        "Nachos": 8.99,
        "Fried Pickles": 6.99,
        "Stuffed Mushrooms": 9.99
    }
    clear_frame()
    appetizer_label = tk.Label(menu_frame, text="Appetizers", font=("Arial", 14))
    appetizer_label.pack()

    for appetizer, price in appetizers.items():
        button = tk.Button(menu_frame, text=f"{appetizer} - ${price}", 
                           command=lambda a=appetizer, p=price: add_to_order(a, p))
        button.pack(pady=2)

    # Main Menu button at the bottom of the screen
    main_menu_button = tk.Button(menu_frame, text="Main Menu", command=show_main_menu)
    main_menu_button.pack(pady=5)

# Create main menu frame
menu_frame = tk.Frame(root)
menu_frame.pack(pady=10)

# Restaurant Information Button
info_button = tk.Button(menu_frame, text="Restaurant Info", command=show_info)
info_button.pack(pady=5)

# Show the main menu on application start
show_main_menu()

# Start the application
root.mainloop()

Last updated