Found 10476 Articles for Python

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

Dev Prakash Sharma
Updated on 19-Jun-2021 08:34:09

5K+ Views

We use the Tkinter Listbox widget to create a list of items. Each item in the listbox has some indexes that are assigned to them sequentially in vertical order.Let us suppose that we want to get the index of a clicked item in the listbox. Then, we have to first create a button that will capture the current selection of the items using list.curselection() method and then, we will print the index using the get() method.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size ... Read More

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

Dev Prakash Sharma
Updated on 19-Jun-2021 08:33:48

20K+ Views

Tkinter text widget is a multiline text input widget. It is used to insert, delete, and add textual data in the input field. It provides many built-in functions and attributes in its widget class.To configure and align the text at the CENTER of a Tkinter Text widget, we can use justify=CENTER property.Example# Import the required libraries 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("tag_name", justify='center') # Insert a Demo Text text.insert("1.0", "How do ... Read More

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

Dev Prakash Sharma
Updated on 19-Jun-2021 08:33:29

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 builtin functions and methods.The method wm_title() is used to change the title of the tkinter window. However, alternatively, we can also use the win.title() method. Generally, wm class provides many methods that let us communicate with the window services.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() ... Read More

How to edit a Listbox item in Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:32:36

4K+ Views

Tkinter Listbox widget is generally used to create a list of items. It can store a list of numbers, characters and support many features like selecting and editing the list items.To edit the Listbox items, we have to first select the item in a loop using listbox.curselection() function and insert a new item after deleting the previous item in the listbox. To insert a new item in the listbox, you can use listbox.insert(**items) function.ExampleIn this example, we will create a list of items in the listbox widget and a button will be used for editing the selected item in the list.# ... Read More

How to make a system tray application in Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:32:06

6K+ Views

A System Tray application is created for the continuous execution of the program. Whenever an application is closed by the user, it will get its state running on the taskbar. To identify a system tray application, we can provide an image or icon to its application.To create a System Tray icon of a tkinter application, we can use pystray module in Python. It has many inbuilt functions and methods that can be used to configure the system tray icon of the application.To install pystray in your machine, you can type "pip install pystray" command in your shell or command prompt.To ... Read More

How to add Label width in Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:31:27

3K+ Views

A Label widget is used to display text and images in an application. The size of the label widget depends on a number of factors such as width, height, and Font-size of the Label text. The height and width define how the label widget should appear in the window.To set the width of the label widget, we should declare the Label widget with a variable. Instantiating the label widget with a variable allows the users to add/edit the properties of the Label widget.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or ... Read More

How to set the height/width of a Label widget in Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:30:02

25K+ Views

The Label widgets are used for displaying text and images in the application. The size of the label widget depends on a number of factors such as width, height, and Font-size of the Label text.The height and width define how the label widget should appear in the window. To set the height and width of the label widget, we should declare the Label widget with a variable. Instantiating the label widget with a variable allows the users to add/edit the properties of the Label widget.Example# Import the required libraries from tkinter import * # Create an instance of tkinter ... Read More

How to stop a loop with a stop button in Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:29:35

8K+ Views

Consider a case of running a process in a loop and we want to stop the loop whenever a button is clicked. Generally, in programming languages, to stop a continuous while loop, we use a break statement. However, in Tkinter, in place of the while loop, we use after() to run the defined function in a loop. To break the continuous loop, use a global Boolean variable which can be updated to change the running state of the loop.For the given example, Create a global variable that works similar to the flag in a loop.Define two buttons, Start and Stop, to start and stop the execution.Define ... Read More

Undo and redo features in a Tkinter Text widget

Dev Prakash Sharma
Updated on 19-Jun-2021 08:29:07

2K+ Views

Tkinter Text widget is another input widget similar to the Entry widget which accepts multiline user input in a text field. It contains many inbuilt features and functions which helps to configure the default properties of the text widget. However, to add undo/Redo features in the Tkinter text widget, we can use Boolean attributes undo which ensures that the text can be retrieved again.Example# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Create a ... Read More

How to use images in Tkinter using photoimage objects?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:28:13

6K+ Views

Python supports PIL or Pillow package which is an open-source library for opening, manipulating, and saving different formats of images in Python projects. We can use it in our Tkinter application to process and display images.The Label widget in Tkinter is used to render text and images in a Tkinter application. To display images with Label widget in a Tkinter application, we can follow these steps, Make sure that Pillow or PIL package is installed in your system.Load the image in a variable using ImageTk.PhotoImage(file=file_location) function.Create a Label widget to assign the image value as the image.Execute the code to ... Read More

Advertisements