Run Multiple Commands When Button is Pressed in Tkinter

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

6K+ 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

Delete All Children's Elements Using Python's Tkinter

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

3K+ 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

Get Coordinates of an Object in a Tkinter Canvas

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

10K+ 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

Change Color of Tkinter Rectangle on Click

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

7K+ 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

Erase Everything from a Tkinter Text Widget

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

1K+ 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

Create Transparent Widgets Using Tkinter

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

25K+ 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

Get Input from a Checkbox in Python Tkinter

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

13K+ 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

Change Default Font for All Widgets in Tkinter

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

3K+ Views

Let us consider a case where we want to change the default font of a Tkinter application. To apply the font and setting it as the default font for a particular application, we have to use option_add(**options) method where we specify a property such as background color, font, etc. The changes made after defining the method will force all the widgets to inherit the same property.ExampleIn the given script, we have set a default font for the application such that it can be used for all the widgets defined in the application.#Import the required libraries from tkinter import * ... Read More

Create a Popup Menu in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 09:20:40

5K+ Views

We need menubars in applications where user interaction is required. Menus can be created by initializing the Menu(parent) object along with the menu items. A popup Menu can be created by initializing tk_popup(x_root, y_root, False) which ensures that the menu is visible on the screen. Now, we will add an event which can be triggered through the Mouse Button (Right Click). The grab_release() method sets the mouse button release to unset the popup menu.Example#Import the required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win = Tk() #Set the geometry of the Tkinter library ... Read More

Configure Tkinter TTK Widgets with Transparent Backgrounds

Dev Prakash Sharma
Updated on 25-May-2021 09:20:18

5K+ Views

Tkinter has many feature attributes and properties to frame the structure of an application and configure the widget. In this article, we will see how to set Tkinter widgets with a transparent background. The wm_attributes('-transparentcolor', 'color') method is used for providing the transparent background to the widget.ExampleIn this example, we will create a Label widget with transparent background.#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x350") #Adding transparent background property win.wm_attributes('-transparentcolor', '#ab23ff') #Create a Label Label(win, text= "Hello World!", font= ('Helvetica 18'), bg= '#ab23ff').pack(ipadx= 50, ... Read More

Advertisements