Programming Articles - Page 1214 of 3363

How to change the color of a Tkinter label programmatically?

Dev Prakash Sharma
Updated on 04-May-2021 14:27:43

17K+ Views

Tkinter Label widgets are used to add text or images to the application. We can even configure the basic properties of labels using the config(options) method. Generally, in order to configure the widgets property dynamically, we use callback functions where we modify the value of attributes.ExampleIn this example, we will modify the color Tkinter Labels by defining the callback function. The function can be activated by a button that forces the labels to change the color.#Import required libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win= Tk() #Define the geometry of ... Read More

How to change font size in ttk.Button?

Dev Prakash Sharma
Updated on 04-May-2021 14:28:22

5K+ Views

Tkinter Ttk is a native library in Tkinter which is used to style the widgets in a Tkinter application. It provides a native GUI interface to all the widgets defined in the application.In order to style the widgets with ttk, we have to import it in the notebook using the command ‘from tkinter import ttk’.For a particular application, we can change the font properties such as background color, foreground color, font size, font-family, and font style by defining an instance of ttk style object. After initializing the ttk object, we can configure(options) each widget defined in an application.ExampleIn this example, ... Read More

How to bind the spacebar key to a certain method in Tkinter?

Dev Prakash Sharma
Updated on 04-May-2021 14:45:24

2K+ Views

Tkinter methods can be bound with the Keys or Mouse to perform certain operations or events in an application. Let us suppose for a particular application, we want to bind the Key such that it will perform a certain operation. We can bind any key to a particular operation or event by defining the bind(, callback) method.ExampleIn this example, we will create rectangles of random width and height by using random Module in Python. So, whenever we will press the Key, it will generate some random shapes on the screen.#Import the required Libraries from tkinter import * import random ... Read More

How to stop Tkinter after function?

Dev Prakash Sharma
Updated on 03-May-2021 09:17:24

6K+ Views

Tkinter functions can be created with the help of threading concept where we define, when a function should run or stop. A Tkinter function can be scheduled using the after(time, callback) function.Let us suppose that we have created a callback function that forces the main window to be closed after some time. There might be times when we need to stop the scheduling of the function. In order to cancel or stop a particular schedule of a callback function, we can use after_cancel(widget) function.ExampleIn the given example, the script will close the main window after 3 seconds but after initializing ... Read More

How to remove focus from a Tkinter widget?

Dev Prakash Sharma
Updated on 03-May-2021 09:13:01

3K+ Views

To active the focus on a particular widget during the execution of a Tkinter program, we can use focus_set() method. Adding the focus creates a gray line around the widget and makes it visible to the user.There might be some cases when we need to remove the focus from the desired widget.This can be done either by removing the focus_set() property or switching the focus from one widget to another.Example#Import the required Libraries from tkinter import * from tkinter import ttk from tkinter import messagebox #Create an instance of Tkinter frame win = Tk() #Set the geometry of Tkinter ... Read More

How do I make a pop-up in Tkinter when a button is clicked?

Dev Prakash Sharma
Updated on 03-May-2021 09:11:08

2K+ Views

Popup window in Tkinter can be created by defining the Toplevel(win) window. A Toplevel window manages to create a child window along with the parent window. It always opens above all the other windows defined in any application. We can create a top-level window or a child window by initializing the object of Toplevel(parent). It inherits all the property of its parent window, such as geometry, title, and width or height.ExampleIn this example, we will create a button that will open a Popup window above all the other windows.#Import the required Libraries from tkinter import * from tkinter import ttk ... Read More

How do I find out the size of a canvas item in Tkinter?

Dev Prakash Sharma
Updated on 03-May-2021 09:07:44

3K+ Views

To find the minimum height and width of canvas items, we can use the bounding box property of Canvas. Basically, a bounding box property enables the canvas item to return the position inside the canvas. Initially, every item in the bbox(item) method defines the minimum and maximum width and height of the item.Example#Import required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Define the geometry of the window win.geometry("750x250") #Initialize a Canvas canvas= Canvas(win, width= 600, height= 200) #Add a text inside the Canvas text= canvas.create_text(300, 20, text= "This is a New Line Text", font=16, ... Read More

How do I create a popup window in Tkinter?

Dev Prakash Sharma
Updated on 03-May-2021 09:05:57

38K+ Views

Popup window in Tkinter can be created by defining the Toplevel(win) window. A Toplevel window manages to create a child window along with the parent window. It always opens above all the other windows defined in any application. We can create a top-level window or child window by initializing the object of Toplevel(parent). It inherits all the property of its parent window such as geometry, title, and width or height.ExampleIn this example, we will create a button that will open a Popup window above all the other windows.#Import the required Libraries from tkinter import * from tkinter import ttk #Create ... Read More

How can I set the default value of my Tkinter Scale widget slider to 100?

Dev Prakash Sharma
Updated on 03-May-2021 09:04:01

1K+ Views

Tkinter Scale Widgets are used to provide the visual representation of data items where we can modify or change the value of data items. In order to change the state of data or select multiple data in the field, we can use the Scale widget.The Scale Widget can be created by initializing a Scale(parent, from_, to, orient, options) constructor.However, sometimes we need to set the default value of the Tkinter Scale widget, which can be set by using the set(numeric_value) method.ExampleIn this example, we will create a scale widget that sets a default value for the slider.#Import the required Libraries ... Read More

How can I put two buttons next to each other in Tkinter?

Dev Prakash Sharma
Updated on 03-May-2021 09:01:35

7K+ Views

Tkinter generally provides three general ways to define the geometry of the widgets. They are − Place, Pack, and Grid Management. If we use the Pack geometry manager, then place the two buttons in the frame using side property. It places the buttons horizontally stacked in the window in (Left, Right, Top and Bottom) direction. The side property maintains the same width and internal padding between all the adjacent widget in the application.Example#Import the required Libraries from tkinter import * from tkinter import ttk import random #Create an instance of Tkinter frame win = Tk() #Set the geometry of Tkinter frame ... Read More

Advertisements