Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Dev Prakash Sharma
Page 25 of 42
How to create Tkinter buttons in a Python for loop?
Tkinter Button widgets are very useful in terms of handling events and performing actions during the execution of an application. We can create Tkinter Buttons using the Button(parent, text, option..) constructor. Using the constructor, we can create multiple buttons within the loop.ExampleIn this example, we will create multiple buttons in the range using a Python for loop.#import required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win= Tk() #Set the geometry of the window win.geometry("750x250") #Create a LabelFrame labelframe= LabelFrame(win) #Define a canvas in the window canvas= Canvas(labelframe) canvas.pack(side=RIGHT, fill=BOTH, ...
Read MoreHow to stop Tkinter after function?
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 MoreHow to set the width of a Tkinter Entry widget in pixels?
Tkinter has an Entry widget to accept single-line user input. It has many properties and attributes that can be used to configure the Entry widget. To change the size (width or height of the Entry widget), we can use Internal Padding Properties – ipadx and ipady. By defining the value of internal padding, we can actually change the width and height of the Entry widget.Example#Import the required Libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x350") #Define a function to submit the validate the ...
Read MoreHow to remove focus from a Tkinter widget?
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 MoreHow do I find out the size of a canvas item in Tkinter?
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 MoreHow can I set the default value of my Tkinter Scale widget slider to 100?
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 MoreHow can I put two buttons next to each other in Tkinter?
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 MoreHow can I insert a string in an Entry widget that is in the "readonly" state using Tkinter?
Tkinter Entry widget is used to insert single-line text input. We can use the Entry widget to create forms where single-line input is the prime requirement.Sometimes, we need to insert a predefined text inside the Entry Widget. In such cases, we can use insert(INSERT, ) method. These are generally called as Placeholders for the Entry widget.Let us suppose that we want to change the state of our text as read-only. Here, we can use state= ‘readonly’ property while configuring the Entry widget.Example#Import the required Libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win = ...
Read MoreConverting Tkinter program to exe file
Let us suppose that we want to create a standalone app (executable application) using tkinter. We can convert any tkinter application to an exe compatible file format using the PyInstaller package in Python.To work with pyinstaller, first install the package in the environment by using the following command, pip install pyinstallerOnce installed, we can follow the steps to convert a Python Script File (contains a Tkinter application file) to an Executable file.Install pyinstaller using pip install pyinstaller in Windows operating system. Now, type pyinstaller --onefile -w filename and press Enter.Now, check the location of the file (script file) and you will ...
Read MoreBinding mouse double click in Tkinter
Let us suppose that for a particular application, we want to bind the mouse double-click so that it performs some event or operation. We can use the bind(‘’, handler) or bind(‘’, handler) methods to bind the mouse Left or Right Buttons with a handler or a callback function.ExampleIn this example, we will create an application that contains a button. When we double click the button, it will open a popup window.#Import required libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win= Tk() #Define the geometry of the window win.geometry("750x250") #Define a function def ...
Read More