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

  1. Simple and easy to learn GUI library in Python.

  2. Provides various widgets such as Button, Label, Entry, Text, Frame, Menu, Canvas, etc.

  3. Supports event-driven programming.

  4. Cross-platform (works on Windows, macOS, and Linux).

  5. 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 Tkinter")
label.pack()

# Run the window
root.mainloop()

Common Tkinter Widgets

1. Label Widget

Used to display text or images on the window.

Example:

from tkinter import *

root = Tk()

label = Label(root, text="Welcome to Tkinter")
label.pack()

root.mainloop()

2. Button Widget

Used to create a clickable button to perform an action.

Example:

from tkinter import *

def click():
print("Button Clicked")

root = Tk()

btn = Button(root, text="Click Me", command=click)
btn.pack()

root.mainloop()

3. Entry Widget

Used to accept single-line text input from the user.

Example:

from tkinter import *

root = Tk()

entry = Entry(root)
entry.pack()

root.mainloop()

4. Text Widget

Used for multi-line text input.

Example:

from tkinter import *

root = Tk()

text = Text(root, height=5, width=30)
text.pack()

root.mainloop()

5. Frame Widget

Used to organize and group other widgets inside a container.

Example:

from tkinter import *

root = Tk()

frame = Frame(root)
frame.pack()

Button(frame, text="Button1").pack()
Button(frame, text="Button2").pack()

root.mainloop()

6. Checkbutton Widget

Used to create a checkbox for selecting options.

Example:

from tkinter import *

root = Tk()

var = IntVar()
check = Checkbutton(root, text="Accept Terms", variable=var)
check.pack()

root.mainloop()

7. Listbox Widget

Used to display a list of selectable items.

Example:

from tkinter import *

root = Tk()

listbox = Listbox(root)
listbox.pack()

listbox.insert(1, "Python")
listbox.insert(2, "Java")
listbox.insert(3, "C++")

root.mainloop()

8. Canvas Widget

Used to draw shapes like lines, rectangles, circles, and graphics.

Example:

from tkinter import *

root = Tk()

canvas = Canvas(root, width=200, height=200)
canvas.pack()

canvas.create_rectangle(50, 50, 150, 150)

root.mainloop()

Comments

Popular posts from this blog

Functions in Python

Python – Database Connection