diff --git a/GUI.ipynb b/GUI.ipynb new file mode 100644 index 0000000..64a3024 --- /dev/null +++ b/GUI.ipynb @@ -0,0 +1,426 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "c264f503", + "metadata": { + "id": "c264f503" + }, + "source": [ + "# GUI Programming with Tkinter\n", + "\n", + "This notebook introduces GUI programming in Python using the `Tkinter` library.\n", + "We will cover:\n", + "1. Basic Window\n", + "2. Adding Labels\n", + "3. Adding Buttons\n", + "4. Entry Widget (User Input)\n", + "5. Frames and Layouts\n", + "6. Menus\n", + "7. Grid Layout\n", + "8. Message Boxes\n", + "9. Final Example: A Simple Calculator\n", + "\n", + "Let's start creating graphical interfaces!\n" + ] + }, + { + "cell_type": "markdown", + "id": "c45e96a5", + "metadata": { + "id": "c45e96a5" + }, + "source": [ + "## 1. Basic Window\n", + "\n", + "We start by creating a simple window using `Tkinter`.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "416d5ca9", + "metadata": { + "id": "416d5ca9" + }, + "outputs": [], + "source": [ + "import tkinter as tk\n", + "\n", + "# Create the main window\n", + "root = tk.Tk()\n", + "\n", + "# Set the window title\n", + "root.title(\"My First GUI\")\n", + "\n", + "# Set window size (width x height)\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "351abaec", + "metadata": { + "id": "351abaec" + }, + "source": [ + "## 2. Adding a Label\n", + "\n", + "Next, we add a label to the window.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78645ec9", + "metadata": { + "id": "78645ec9" + }, + "outputs": [], + "source": [ + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Label Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create a label\n", + "label = tk.Label(root, text=\"Hello, World!\")\n", + "label.pack()\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "1a59f584", + "metadata": { + "id": "1a59f584" + }, + "source": [ + "## 3. Adding a Button\n", + "\n", + "We will add a button that responds when clicked.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa7a7f72", + "metadata": { + "id": "fa7a7f72" + }, + "outputs": [], + "source": [ + "# Function to handle button click\n", + "def on_button_click():\n", + " label.config(text=\"Button clicked!\")\n", + "\n", + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Button Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create a label\n", + "label = tk.Label(root, text=\"Press the button\")\n", + "label.pack()\n", + "\n", + "# Create a button\n", + "button = tk.Button(root, text=\"Click me\", command=on_button_click)\n", + "button.pack()\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "c38a77c1", + "metadata": { + "id": "c38a77c1" + }, + "source": [ + "## 4. Entry Widget (User Input)\n", + "\n", + "We add an entry widget that allows user input.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75a37e4d", + "metadata": { + "id": "75a37e4d" + }, + "outputs": [], + "source": [ + "# Function to display the entered text\n", + "def show_entry():\n", + " label.config(text=\"You entered: \" + entry.get())\n", + "\n", + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Entry Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create a label\n", + "label = tk.Label(root, text=\"Enter something:\")\n", + "label.pack()\n", + "\n", + "# Create an entry widget\n", + "entry = tk.Entry(root)\n", + "entry.pack()\n", + "\n", + "# Create a button to submit\n", + "button = tk.Button(root, text=\"Submit\", command=show_entry)\n", + "button.pack()\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "8608ff2a", + "metadata": { + "id": "8608ff2a" + }, + "source": [ + "## 5. Frames and Layout\n", + "\n", + "Frames help organize the layout of your GUI.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8cecdd6a", + "metadata": { + "id": "8cecdd6a" + }, + "outputs": [], + "source": [ + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Frame Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create a frame\n", + "frame = tk.Frame(root)\n", + "frame.pack()\n", + "\n", + "# Add widgets to the frame\n", + "label = tk.Label(frame, text=\"This is inside a frame\")\n", + "label.pack()\n", + "\n", + "button = tk.Button(frame, text=\"Click me\")\n", + "button.pack()\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "01ed66c3", + "metadata": { + "id": "01ed66c3" + }, + "source": [ + "## 6. Menus\n", + "\n", + "Let's create a simple menu in the window.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cb2afc1", + "metadata": { + "id": "5cb2afc1" + }, + "outputs": [], + "source": [ + "# Function to respond to menu options\n", + "def menu_item_click():\n", + " label.config(text=\"Menu item clicked\")\n", + "\n", + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Menu Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create a label\n", + "label = tk.Label(root, text=\"Menu Example\")\n", + "label.pack()\n", + "\n", + "# Create a menu bar\n", + "menu_bar = tk.Menu(root)\n", + "\n", + "# Create a file menu\n", + "file_menu = tk.Menu(menu_bar, tearoff=0)\n", + "file_menu.add_command(label=\"Open\", command=menu_item_click)\n", + "file_menu.add_command(label=\"Save\", command=menu_item_click)\n", + "file_menu.add_separator()\n", + "file_menu.add_command(label=\"Exit\", command=root.quit)\n", + "\n", + "# Add the file menu to the menu bar\n", + "menu_bar.add_cascade(label=\"File\", menu=file_menu)\n", + "\n", + "# Display the menu bar\n", + "root.config(menu=menu_bar)\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "92eb51fc", + "metadata": { + "id": "92eb51fc" + }, + "source": [ + "## 7. Grid Layout\n", + "\n", + "Using `grid()` to arrange widgets.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4eeb8d9c", + "metadata": { + "id": "4eeb8d9c" + }, + "outputs": [], + "source": [ + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Grid Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create labels and entries in a grid\n", + "tk.Label(root, text=\"Name:\").grid(row=0, column=0)\n", + "tk.Entry(root).grid(row=0, column=1)\n", + "\n", + "tk.Label(root, text=\"Age:\").grid(row=1, column=0)\n", + "tk.Entry(root).grid(row=1, column=1)\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "10b27c2b", + "metadata": { + "id": "10b27c2b" + }, + "source": [ + "## 8. Message Boxes\n", + "\n", + "Creating message boxes for pop-up dialogs.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17794a86", + "metadata": { + "id": "17794a86" + }, + "outputs": [], + "source": [ + "from tkinter import messagebox\n", + "\n", + "# Function to show message box\n", + "def show_message():\n", + " messagebox.showinfo(\"Information\", \"This is a message box!\")\n", + "\n", + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Message Box Example\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create a button to show message box\n", + "button = tk.Button(root, text=\"Show Message\", command=show_message)\n", + "button.pack()\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + }, + { + "cell_type": "markdown", + "id": "d9d52404", + "metadata": { + "id": "d9d52404" + }, + "source": [ + "## 9. Final Example: A Simple Calculator\n", + "\n", + "Let's put everything together and create a simple calculator using Tkinter.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f9eacbbb", + "metadata": { + "id": "f9eacbbb" + }, + "outputs": [], + "source": [ + "# Function to calculate the result\n", + "def calculate():\n", + " try:\n", + " result = eval(entry.get())\n", + " label.config(text=\"Result: \" + str(result))\n", + " except:\n", + " label.config(text=\"Error\")\n", + "\n", + "# Create the main window\n", + "root = tk.Tk()\n", + "root.title(\"Simple Calculator\")\n", + "root.geometry(\"300x200\")\n", + "\n", + "# Create an entry widget for input\n", + "entry = tk.Entry(root)\n", + "entry.pack()\n", + "\n", + "# Create a label for displaying the result\n", + "label = tk.Label(root, text=\"Result: \")\n", + "label.pack()\n", + "\n", + "# Create a button to perform calculation\n", + "button = tk.Button(root, text=\"Calculate\", command=calculate)\n", + "button.pack()\n", + "\n", + "# Start the GUI event loop\n", + "root.mainloop()\n" + ] + } + ], + "metadata": { + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file