Posts

Showing posts from March, 2026

Layout Managers in Tkinter

 In Tkinter , a Layout Manager is used to arrange and position widgets (buttons, labels, text boxes, etc.) inside a window . Tkinter provides three main layout managers : Pack() Place() Grid() 1. Pack Layout Manager The pack() method organizes widgets vertically or horizontally in blocks. It automatically adjusts the position of widgets inside the window. Features Simple and easy to use Widgets are arranged top, bottom, left, or right Example Program from tkinter import * root = Tk () Label ( root , text = "First Label" ) . pack() Label ( root , text = "Second Label" ) . pack() Label ( root , text = "Third Label" ) . pack() root . mainloop() Output: Three labels will appear one below another in the window. 2. Place Layout Manager The place() method positions widgets using specific x and y coordinates . It gives exact control over the widget position. Features Uses absolute positioning Allows setting x and y va...

Tkinter -Python

  Introduction to Tkinter (Python) Tkinter is the standard GUI (Graphical User Interface) library in Python . It is used to create desktop applications with graphical elements like windows, buttons, labels, text boxes, menus, and dialogs . Tkinter is built on top of the Tk GUI toolkit and is included with most Python installations, so it does not require separate installation. It allows programmers to easily develop interactive applications using Python. Features of Tkinter Simple and easy to learn GUI library in Python. Provides various widgets such as Button, Label, Entry, Text, Frame, Menu, Canvas, etc. Supports event-driven programming. Cross-platform (works on Windows, macOS, and Linux). Useful for creating small desktop applications and GUI tools. Basic Example Program from tkinter import * # Create main window root = Tk () root . title( "My First Tkinter Program" ) # Create label label = Label ( root , text = "Hello Welcome to Tkinte...

Exception Handling

  Exception Handling in Python 1. Built-in Exceptions Python provides many predefined exceptions that occur during program execution when an error happens. Common built-in exceptions include: Exception Description ZeroDivisionError         Occurs when dividing by zero TypeError         Occurs when wrong data type is used ValueError         Occurs when invalid value is given IndexError         Occurs when index is out of range KeyError         Occurs when dictionary key is not found FileNotFoundError         Occurs when file does not exist Example: a = 10 b = 0 print ( a / b ) Output: ZeroDivisionError: division by zero 2. Handling Exceptions Exceptions can be handled using try and except blocks . Syntax try : # code that may cause error except ExceptionType : # handling code Example try : a = int ( input ( "Enter a number: " )) ...

Python – Database Connection

 Python can connect to databases like MySQL, SQLite, PostgreSQL, Oracle etc. Database connectivity allows us to: Connect to database Create tables Insert data Update data Delete data Read data Control transactions Disconnect properly For example, we commonly use MySQL with Python using the connector: pip install mysql-connector-python  1. Database Connection To connect Python with MySQL database: import mysql . connector conn = mysql . connector . connect( host = "localhost" , user = "root" , password = "yourpassword" , database = "college" ) print ( "Connected Successfully" ) If connection is successful, it will print message.  2. Creating Cursor Object Cursor is used to execute SQL queries. cursor = conn . cursor() Cursor acts as a bridge between Python and database.  3. Creating Table create_table_query = """ CREATE TABLE IF NOT EXISTS students ( id IN...

File Handling in Python

 File handling in Python is used to store data permanently in a file. Normally, variables store data temporarily in RAM. But when the program ends, data is lost. To store data permanently, we use files. Python provides built-in functions to: Create files Read files Write files Append data Delete files Opening a File To work with files, first we must open the file using the open() function. Syntax: open ( "filename" , "mode" ) Example: file = open ( "data.txt" , "r" ) File Modes in Python Mode Description r                             Read mode (default) w                   Write mode (creates new file or overwrites) a                   Append mode (adds data at end) x                   Create new file (error if exists) rb       ...