Bundle a Python Tkinter Application Including Dependencies

Dev Prakash Sharma
Updated on 26-Mar-2021 11:21:10

891 Views

Let us suppose we have created a tkinter application and now, we want to bundle the standalone application to make it portable and executable. We can use different Python packages that support various functionality to bundle the whole application code into an executable installer. These packages compress the code and convert the standalone application into an executable code.For a Windows-based user, we can use py2exe; for Linux, we can use Freeze; and for Mac, we can use py2app.ExampleIn this example, we have created a Windows-based application that prints “Hello World” on the screen. Initially, we will create a setup.py file ... Read More

Center a Window on the Screen in Tkinter

Dev Prakash Sharma
Updated on 26-Mar-2021 11:18:04

22K+ Views

In order to place a tkinter window at the center of the screen, we can use the PlaceWindow method in which we can pass the toplevel window as an argument and add it into the center.We can also set the window to its center programmatically by defining its geometry.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x250") win.eval('tk::PlaceWindow . center') win.mainloop()OutputRunning the above code will create a centered window.

Change Tkinter Widget's Font Style Without Knowing Font Family Size

Dev Prakash Sharma
Updated on 26-Mar-2021 11:16:41

346 Views

Tkinter’s widgets support properties and attributes such as font-family and font size which can be specified using the font(‘Font-Family’, font-size) property.ExampleIn the following example, we have created a text label that can be configured by defining the font-family as “Times New Roman” and the font-size as “20”.#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("650x250") #Add a text label and add the font property to it label= Label(win, text= "This is a New Text", font=('Times New Roman bold', 20)) label.pack(padx=10, pady=10) win.mainloop()OutputRunning the above ... Read More

Change Color of Words in Tkinter Text Widget

Dev Prakash Sharma
Updated on 26-Mar-2021 11:16:15

11K+ Views

Tkinter text widgets are used to create and display multiline text Input. It provides several functions and methods that are generally used to configure a text widget.Let us suppose we want to change the color of certain words in a text widget, then we can use the tag_add(tag name, range) method which selects the word we want to format. Once the word has been selected, we can change its color, background color, and other properties using the tag_config(properties) method.ExampleIn this example, we will configure the color of a selected word in the text widget.#Import required libraries from tkinter import * ... Read More

Change Tkinter Button State from Disabled to Normal

Dev Prakash Sharma
Updated on 26-Mar-2021 11:15:51

1K+ Views

Tkinter provides Button widgets to create a button for triggering an event. Let us suppose we have created a button that is already disabled in an application. In order to change the state of the button, we can use the state property.The state property is used to enable and disable a button in an application. In order to change the state of the application, we have two operations: state=DISABLED and state=NORMAL.Example#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("650x450") #Define a function for Button ... Read More

Clear Entire Treeview with Tkinter

Dev Prakash Sharma
Updated on 26-Mar-2021 11:11:06

6K+ Views

Tkinter Treeview widgets are used to display the hierarchy of the items in the form of a list. It generally looks like the file explorer in Windows or Mac OS.Let us suppose we have created a list of items using treeview widget and we want to clear the entire treeview, then we can use the delete() function. The function can be invoked while iterating over the treeview items.ExampleIn this example, we will create a treeview for the Programming Language and will clear the list of items using delete() operation.#Import the required library from tkinter import * from tkinter import ttk ... Read More

Clear Out a Frame in Tkinter

Dev Prakash Sharma
Updated on 26-Mar-2021 11:09:19

24K+ Views

Tkinter frames are used to group and organize too many widgets in an aesthetic way. A frame component can contain Button widgets, Entry Widgets, Labels, ScrollBars, and other widgets.If we want to clear the frame content or delete all the widgets inside the frame, we can use the destroy() method. This method can be invoked by targeting the children of the frame using winfo_children().Example#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("600x250") #Create a frame frame = Frame(win) frame.pack(side="top", expand=True, fill="both") #Create a ... Read More

Clear Contents of a Tkinter Text Widget

Dev Prakash Sharma
Updated on 26-Mar-2021 11:08:12

23K+ Views

Tkinter Text Widget is used to add the text writer in an application. It has many attributes and properties which are used to extend the functionality of a text editor. In order to delete the input content, we can use the delete("start", "end") method.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x250") #Define a function to clear the input text def clearToTextInput():    my_text.delete("1.0", "end") #Create a text widget my_text=Text(win, height=10) my_text.pack() #Create a Button btn=Button(win, height=1, width=10, text="Clear", command=clearToTextInput) btn.pack() #Display ... Read More

Clear Tkinter Canvas

Dev Prakash Sharma
Updated on 26-Mar-2021 11:05:41

25K+ Views

Tkinter provides a way to add a canvas in a window and when we create a canvas, it wraps up some storage inside the memory. While creating a canvas in tkinter, it will effectively eat some memory which needs to be cleared or deleted.In order to clear a canvas, we can use the delete() method. By specifying “all”, we can delete and clear all the canvas that are present in a tkinter frame.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("650x250") #Creating a canvas myCanvas =Canvas(win, ... Read More

Close Tkinter Window by Pressing a Button

Dev Prakash Sharma
Updated on 26-Mar-2021 11:01:23

2K+ Views

Tkinter initially creates a window or frame that contains widgets and Labels within it. Let us suppose we want to close the tkinter window with a button. A Button is a UI widget that can be used to perform a certain operation.ExampleHere, we will create a button that closes the tkinter window. In order to close and terminate the TCL interpreter, we mainly use the destroy() method.#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("600x250") #Create a Label Label(win, text="Press the Close Button to ... Read More

Advertisements