Found 604 Articles for Tkinter

Determine which Button was pressed in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 09:52:21

5K+ Views

Buttons are very useful in many applications where user interaction is required. Let us suppose that we want to know which button is pressed in a given application. In order to get the information about the Button, we can use the callback function in the Button configuration. In the Callback function, we will use the print(test) function to print the button that is clicked.Example#Import the required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x250") # Define function to get the information about the ... Read More

Creating a transparent background in a Tkinter window

Dev Prakash Sharma
Updated on 25-May-2021 09:51:56

7K+ Views

Tkinter window provides many inbuilt functions and properties to help an application work seamlessly. They configure the GUI of the application as well.If we want to create a transparent window in an application, then we should have to define the color in the attributes('-transparentcolor', 'color' ) method. By providing the color of the window and widget, it will make the window transparent.Example#Import the Tkinter Library from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry of window win.geometry("700x350") #Add a background color to the Main Window win.config(bg = '#add123') #Create ... Read More

Running multiple commands when a button is pressed in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 09:51:29

5K+ Views

The Button widget provides a way to communicate through all the existing functionalities of an application. We can perform a certain action with the help of a Button that encapsulates the function and the objects. However, there might be cases when we want to perform multiple operations with a single button. This can be achieved by defining the lambda functions which target multiple events or callback in the application.ExampleIn this example, we will add multiple events to a specific Button.#Import the Tkinter Library from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the ... Read More

How to delete all children's elements using Python's Tkinter?

Dev Prakash Sharma
Updated on 25-May-2021 09:51:00

2K+ Views

Frames are very useful in a Tkinter application. If we define a Frame in an application, it means we have the privilege to add a group of widgets inside it. However, all these widgets are called Children of that particular Frame.Let us suppose that we want to remove all the children widgets defined in a frame. Then, first we have to get the focus on children using the winfo_children() method. Once we get the focus, we can delete all the existing children using destroy() method.Example#Import the Tkinter Library from tkinter import * #Create an instance of Tkinter Frame win ... Read More

How to get the coordinates of an object in a Tkinter canvas?

Dev Prakash Sharma
Updated on 25-May-2021 09:50:31

7K+ Views

Tkinter Canvas Widget provides GUI features to an application. It can be used to draw shapes, animate objects, and configure the existing items in a canvas. Whenever we create shapes, we have to provide the size and coordinates of the shapes in the Canvas item constructor. In order to return the coordinates of an item on the Canvas, we can use the coords(item) method. It returns a list with the coordinates of the shapes in the canvas widget.Examplefrom tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry of Tkinter frame win.geometry("700x250") # Initialize a ... Read More

Return the input of the Entry widget in Tkinter

Dev Prakash Sharma
Updated on 13-Sep-2023 14:34:15

34K+ Views

An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.ExampleThe following example will return the input data that can be used to display in the window with the help of a Label Widget as well.#Import the required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter Frame win = Tk() #Set the ... Read More

How to change the color of a Tkinter rectangle on clicking?

Dev Prakash Sharma
Updated on 25-May-2021 09:49:39

6K+ Views

The Canvas widget is one of the most versatile widgets in the Tkinter Library. It is used for creating shapes of different types and sizes, animating objects, visualizing graphics, and many more. To change the property of a particular item in Tkinter, we can use itemconfig(**options) method. It takes options such as background color, outline color, and other useful properties of the items defined in a canvas.ExampleIn this example, we will create a rectangle such that the color inside the rectangle would change after clicking a Button.# Import the required libraries from tkinter import * from tkinter import ttk ... Read More

How to erase everything from a Tkinter text widget?

Dev Prakash Sharma
Updated on 25-May-2021 09:49:19

840 Views

Tkinter Text widget is more than just a multiline Entry widget. It supports the implementation of multicolored text, hyperlink text, and many more.Let us suppose a text widget has been created in an application. Now, to clear the Text widget, we can use the delete("1.0", END) method. It can be invoked in a callback function or event which can be triggered through an object of the Button Class.Example# Import the required libraries from tkinter import * # Create an instance of Tkinter Frame win = Tk() # Set the geometry win.geometry("750x250") # Define a function to clear ... Read More

How to create transparent widgets using Tkinter?

Dev Prakash Sharma
Updated on 25-May-2021 09:48:57

20K+ Views

A Tkinter widget in an application can be provided with Transparent background. The background property of any widget is controlled by the widget itself.However, to provide a transparent background to a particular widget, we have to use wm_attributes('transparentcolor', 'colorname') method. It works in the widget only after adding the same transparent color as the background color of the widget.Example#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x250") #Adding transparent background property win.wm_attributes('-transparentcolor', '#ab23ff') #Create a Label Label(win, text= "This is a New line Text", font= ... Read More

How to get the input from a Checkbox in Python Tkinter?

Dev Prakash Sharma
Updated on 25-May-2021 09:21:28

12K+ Views

A checkbox widget is an input widget that has two values, either True or False. A checkbox is useful in many applications where a particular value needs to be validated.Let us suppose that we want to get the Input value from a checkbox such that if it is selected, then print the selected value. To print the value of the selected checkbox, we can use the get() method. It returns the input value of a particular widget.Example# Import Tkinter library from tkinter import * # Create an instance of tkinter frame win = Tk() # Set the geometry of ... Read More

Advertisements