Python is one of the most popular programming languages in the world, known for its simplicity, readability, and vast ecosystem. Among the many tools and libraries available, widgets play a crucial role in developing interactive applications—especially for GUIs (Graphical User Interfaces), dashboards, and notebooks.
In this post, we’ll explore what python widgets are, where they are used, and how to create and use them effectively.
Getting Started
Widgets are standard graphical user interface (GUI) elements and nothing but controls like button, canvas, grid, etc. which we are using in Windows application in .Net Technology. Python provides various widgets like other technology for windows applications.
Widgets are interactive elements in a user interface that allow users to interact with an application. Common examples include:
- Button: The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.
- Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets.
- Check Button: To select any number of options by displaying a number of options to a user as toggle buttons.
- Entry: It is used to accept inputs text from user the single line text entry from the user.
- Text: It is similar like Entry, but it accepts multiple lines.
- Label : A Label widget shows text to the user. You can update the widget programmatically too.
- Frame :It acts as a container to hold the widgets. It is used for grouping and organizing the widgets.
- List box: It offers a list to the user from which the user can accept any number of options.
- Message: The widget can be used to display short text messages. The message widget is similar in its functionality to the Label widget, but it is more flexible in displaying text.
- Radio Button: It is used to offer multi-choice option to the user. It offers several options to the user and the user has to choose one option.
- Scale: It is nothing but slider that allows to select any value from that scale.
- Scrollbar: It refers to the slide controller which will be used to implement listed widgets.
- Panned Window: It is a container widget which is used to handle number of panes arranged in it.
- Spin Box: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.
- GUI applications (via libraries like
tkinter
,PyQt
, orKivy
) - Web applications (via frameworks like
Dash
orStreamlit
) - Jupyter Notebooks (via
ipywidgets
)
Popular Python Widget Libraries
Tkinter
Tkinter is the standard Graphical User Interface (GUI) library for Python. It provides tools to create desktop applications with windows, buttons, text boxes, labels, and other interactive widgets.
Tkinter is a thin object-oriented layer on top of Tcl/Tk, a GUI toolkit developed in the early 1990s. Because it comes bundled with Python (no need for extra installation), it's a popular choice for beginners and hobbyists who want to build simple GUI applications.
Example import tkinter as tk
def say_hello():
print("Hello, World!")
# Create the main window
root = tk.Tk()
root.title("My First Tkinter App")
root.geometry("300x200") # Width x Height
# Create a button and place it in the window
button = tk.Button(root, text="Click Me", command=say_hello)
button.pack(pady=20)
# Run the application
root.mainloop()
ipywidgets
ipywidgets
(short for IPython widgets) is a library that helps you create interactive widgets in Jupyter Notebooks or JupyterLab. These widgets let you interact with your code and data in real-time through sliders, buttons, dropdowns, text boxes, and more—right in the notebook.
from ipywidgets import interact
def greet(name):
print(f"Hello, {name}!")
interact(greet, name="World")
Streamlit
Streamlit
is an open-source Python library that lets you turn your Python scripts into interactive web apps. It's perfect for data scientists, ML engineers, and analysts who want to build and share dashboards, models, and tools quickly using only Python.
import streamlit as st
name = st.text_input("Enter your name:")
if name:
st.write(f"Hello, {name}!")
Dash by Plotly
Dash
by Plotly is a powerful Python framework for building interactive web applications focused on data visualization and analytics. It’s widely used to create dashboards, complex data apps, and enterprise-grade web apps
Summary
In the above, we say how to buil gui using python, how to use gui application in python and the various commonly python widgets. I hope you have enjoyed it a lot.
Example import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
app = dash.Dash(__name__)
# Sample DataFrame
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})
app.layout = html.Div([
dcc.Dropdown(
id='city-dropdown',
options=[{'label': c, 'value': c} for c in df['City'].unique()],
value='SF'
),
dcc.Graph(id='bar-chart')
])
@app.callback(
Output('bar-chart', 'figure'),
[Input('city-dropdown', 'value')]
)
def update_chart(selected_city):
filtered_df = df[df['City'] == selected_city]
fig = px.bar(filtered_df, x='Fruit', y='Amount', title=f'Fruit Amount in {selected_city}')
return fig
if __name__ == '__main__':
app.run_server(debug=True)
Summary
Python widgets empower developers to build interactive and user-friendly applications quickly and efficiently. By understanding and utilizing tools like tkinter
, ipywidgets
, Streamlit
, and Dash
, you can enhance your projects and create more engaging experiences for users.
Thanks