GUI-Programming Articles

Page 10 of 25

Drawing a line between two mouse clicks using tkinter

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

Drawing lines between mouse clicks is a common GUI programming task. Tkinter provides event handling capabilities that allow us to capture mouse clicks and draw graphics on a canvas widget. To draw a line between two points, we can follow these general steps ? Create a canvas widget and pack it to display in the window. Define a function draw_line() that works as the event handler when the user clicks. Create a global variable that counts the number of clicks in the canvas. If the count becomes two, then draw a line between the first and second ...

Read More

How to change the width of a Frame dynamically in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 26-Mar-2026 26K+ Views

The Frame widget in Tkinter works like a container where we can place widgets and all the other GUI components. To change the frame width dynamically, we can use the configure() method and define the width property in it. Example In this example, we have created a button that is packed inside the main window and whenever we click the button, it will update the width of the frame ? # Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win = Tk() ...

Read More

How to draw a scale that ranges from red to green in Tkinter?

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

A color gradient defines the range of position-dependent colors. To create a rectangular scale in Tkinter that transitions from red to green, we need to create a canvas widget and fill it with gradually changing colors using mathematical calculations. Steps to Create a Red-to-Green Scale Create a rectangle using a Canvas widget and define its dimensions Define a function to convert RGB values to hexadecimal color codes Calculate the color transition values for each position Iterate through the range and fill rectangles with the calculated colors Example # Import the required libraries from ...

Read More

How to hide and show canvas items on Tkinter?

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

The Canvas widget is one of the versatile widgets in Tkinter. It is used in many applications for designing the graphical user interface such as designing, adding images, creating graphics, etc. We can add widgets in the Canvas widget itself. The widgets that lie inside the canvas are sometimes called Canvas Items. If we want to show or hide the canvas items through a Button, then this can be achieved by using the state property in itemconfig(id, state) method. Syntax The basic syntax for hiding and showing canvas items is − canvas.itemconfig(item_id, state='hidden') # ...

Read More

How do I get the index of an item in Tkinter.Listbox?

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

We use the Tkinter Listbox widget to create a list of items. Each item in the listbox has sequential indexes assigned to them in vertical order starting from 0. To get the index of a clicked item in the listbox, we use the curselection() method which returns a tuple of selected item indexes. We can then access specific items using the get() method. Basic Example Here's how to get the index of selected items in a Listbox ? # Import the required libraries from tkinter import * # Create an instance of tkinter frame ...

Read More

How do I center the text in a Tkinter Text widget?

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

Tkinter's Text widget is a multiline text input widget used for inserting, deleting, and adding textual data. To center-align text in a Text widget, you need to use tags with the justify='center' property. Basic Text Centering Use tag_configure() to create a tag with center justification, then apply it to your text ? from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size of the window win.geometry("700x350") text = Text(win) # Configure the alignment of the text text.tag_configure("center", justify='center') # Insert a ...

Read More

Difference between title() and wm_title() methods in Tkinter class

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

Tkinter has a definite class hierarchy which contains many functions and built-in methods. As we create applications, we use these functions to build the structure of components. The wm class stands for "window manager" that is a mixin class that provides many built-in functions and methods. The method wm_title() is used to change the title of the tkinter window. However, alternatively, we can also use the title() method. Both methods achieve the same result, but they differ in their implementation approach. Understanding wm_title() and title() Both wm_title() and title() methods set the window title, but title() is ...

Read More

How to make a system tray application in Tkinter?

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

A System Tray application runs continuously in the background, remaining accessible through the system taskbar even when the main window is closed. This is useful for applications that need to run persistently while minimizing screen space usage. To create a System Tray application in Tkinter, we use the pystray module, which provides functions to create and manage system tray icons with contextual menus. Installation First, install the required package using pip ? pip install pystray Creating a System Tray Application Here's how to create a Tkinter application that minimizes to the system ...

Read More

Undo and redo features in a Tkinter Text widget

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

Tkinter Text widget is a multiline input widget that accepts text input from users. To add undo and redo functionality, we need to enable the undo parameter and use specific keyboard shortcuts or methods. Basic Undo/Redo Setup Enable undo functionality by setting undo=True when creating the Text widget ? import tkinter as tk # Create main window root = tk.Tk() root.title("Undo/Redo Example") root.geometry("500x400") # Create Text widget with undo enabled text_widget = tk.Text(root, width=50, height=15, undo=True) text_widget.pack(pady=20) # Insert some initial text text_widget.insert(tk.END, "Type here and try Ctrl+Z (undo) or Ctrl+Y (redo)") ...

Read More

How to set the position of a Tkinter window without setting the dimensions?

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

Tkinter windows are executed after initializing the object of the Tkinter frame or window. We can define the size of the Tkinter window or frame using the geometry manager. It defines the width and height of the initial Tkinter window where we generally place our widgets. To set the position of the Tkinter window while omitting the width and height, we can define the specification in the geometry manager. Understanding Window Positioning The geometry() method accepts a string in the format "widthxheight+x+y". When you omit the width and height, you can use just "+x+y" to position the window ...

Read More
Showing 91–100 of 242 articles
« Prev 1 8 9 10 11 12 25 Next »
Advertisements