GUI-Programming Articles

Page 12 of 25

How to create a Tkinter toggle button?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 5K+ Views

Python's Tkinter library provides a comprehensive set of widgets for creating GUI applications. A toggle button is a special type of button that switches between two states when clicked, commonly used for on/off functionality like enabling night mode or day mode in applications. To create a toggle button in Tkinter, we use a regular Button widget with custom images and a function that changes the button's state and appearance when clicked. The key is using a global variable to track the current state. Creating a Basic Toggle Button Here's how to create a toggle button that switches ...

Read More

What is the difference between focus and focus_set methods in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 2K+ Views

In Tkinter, focus refers to which widget is currently accepting keyboard input. The focus() and focus_set() methods are used to programmatically set focus to a widget, making it active for user input. Key Difference Both focus() and focus_set() perform the same function – they are identical methods. The focus_set() method is simply an alias for focus(), provided for clarity and consistency with other Tkinter naming conventions. Syntax widget.focus() # OR widget.focus_set() Example Here's how to use both methods to set focus on an Entry widget ? import tkinter as tk ...

Read More

How can I change the text of the Tkinter Listbox item?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 1K+ Views

To display a list of items in the application, Tkinter provides a Listbox widget. It is used to create a list of items vertically. When we want to change the text of a specific Listbox item, we have to first select the item by iterating over the listbox.curselection() and insert a new item after deletion. To insert an item in the list, you can use listbox.insert(). Example Here's a complete example that demonstrates how to change the text of a selected Listbox item ? # Import the required libraries from tkinter import * from tkinter import ...

Read More

Delete and Edit items in Tkinter TreeView

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 18K+ Views

Tkinter Treeview widget is used to display data in a hierarchical structure where each row can represent a file or directory. The Treeview widget items can be edited and deleted by selecting them using tree.selection() function and then performing operations on the selected items. Creating a Treeview Widget To create a Treeview widget, we use the Treeview(parent, columns) constructor ? import tkinter as tk from tkinter import ttk # Create the main window root = tk.Tk() root.geometry("700x350") root.title("Treeview Edit and Delete Example") # Create a Treeview widget tree = ttk.Treeview(root, columns=("c1", "c2"), show='headings', height=8) ...

Read More

How to get coordinates on scrollable canvas in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 2K+ Views

The canvas widget in Tkinter has two coordinate systems: window coordinates and canvas coordinates. Window coordinates always start from (0, 0) at the top-left corner of the visible window, while canvas coordinates represent the actual position of items within the entire canvas area, including scrolled regions. When working with scrollable canvases, mouse events return window coordinates, but you often need canvas coordinates to place or interact with canvas items correctly. Converting Window to Canvas Coordinates Tkinter provides two methods to convert window coordinates to canvas coordinates: canvas.canvasx(event.x) # Convert x coordinate canvas.canvasy(event.y) # ...

Read More

What is correct: widget.rowconfigure or widget.grid_rowconfigure in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 6K+ Views

When working with Tkinter's Grid geometry manager, you'll encounter two similar methods: widget.rowconfigure() and widget.grid_rowconfigure(). Both methods configure how rows behave in the grid layout, particularly for weight distribution and resizing behavior. The Answer: Both Are Correct widget.rowconfigure() and widget.grid_rowconfigure() are identical methods. The rowconfigure() method is simply an alias for grid_rowconfigure(). You can use either one − they produce exactly the same result. Syntax widget.rowconfigure(row_index, weight=value) # OR widget.grid_rowconfigure(row_index, weight=value) Parameters row_index: The row number to configure (starting from 0) weight: How much extra space this row should take when ...

Read More

How can I play a sound when a Tkinter button is pushed?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 3K+ Views

Python's Tkinter and Pygame modules can be combined to create GUI applications that play sounds. Pygame provides excellent audio handling capabilities, while Tkinter offers the interface components. Prerequisites Before starting, ensure Pygame is installed ? pip install pygame Basic Sound Player with Button Here's a complete example that plays a sound when you click a button ? import tkinter as tk import pygame import os # Create main window root = tk.Tk() root.title("Sound Player") root.geometry("300x150") # Initialize pygame mixer pygame.mixer.init() def play_sound(): """Function to ...

Read More

How to make a Button Hover to change the Background Color in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 1K+ Views

A Button widget in Tkinter has many built-in features which can be used to configure and perform certain tasks in the application. In order to run a particular event in the application, we can use the bind("", callback) method to bind a function or event with the button. To add the hover property in the Button, we can use and parameters in the bind function. Example Here's how to create a button that changes the background color when hovering ? # Import the required libraries from tkinter import * from tkinter import ttk ...

Read More

How to bind multiple events with one "bind" in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 4K+ Views

In Tkinter applications, you often need to bind multiple events to the same widget or bind the same events to multiple widgets. The bind() method allows you to connect events like mouse clicks, key presses, or hover actions to callback functions. Understanding Event Binding The basic syntax for binding events is widget.bind(event, callback). To bind multiple events to multiple widgets efficiently, you can iterate over a list of widgets and apply the same bindings. Example: Binding Multiple Events to Multiple Widgets Here's how to bind hover events to multiple buttons that affect a label's appearance ? ...

Read More

How to open an Excel Spreadsheet in Treeview widget in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 8K+ Views

An Excel Spreadsheet contains data organized in rows and columns. You can display spreadsheet data in a Tkinter application using the Treeview widget, which allows tabular data presentation. The Pandas library provides powerful tools for reading and manipulating Excel files in Python. This tutorial demonstrates how to create a Tkinter application that opens Excel files and displays their contents in a Treeview widget with a file menu interface. Required Libraries You'll need to import the following libraries ? tkinter ? GUI framework for creating the interface pandas ? Reading and processing Excel files filedialog ? ...

Read More
Showing 111–120 of 242 articles
« Prev 1 10 11 12 13 14 25 Next »
Advertisements