Found 605 Articles for Tkinter

How do I change the background of a Frame in Tkinter?

Dev Prakash Sharma
Updated on 27-Mar-2021 06:19:15

11K+ Views

In order to change the background color and foreground color of a tkinter frame, we can assign different values to the bg and fg parameters in the Frame function.ExampleIn this example, we have created two frames with different background colors.#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("650x250") #Create an frame frame1= Frame(win, bg= "red") frame2= Frame(win, bg="black") #Create an label inside the frame Label(frame2, text= "Line:1", font=('Lucida font', 20)).pack(pady=20) Label(frame1, text= "Line:2", font=('Lucida font', 20)).pack(pady=20) frame1.pack() frame2.pack() win.mainloop()OutputRunning the above ... Read More

How do I display tooltips in Tkinter?

Dev Prakash Sharma
Updated on 27-Mar-2021 06:10:28

3K+ Views

Tooltips are useful in applications where we need to display some information while hovering on a button.In order to create and display a tooltip, we can use the Balloon property of tkinter.Example#Import the tkinter library from tkinter import * from tkinter.tix import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x450") #Create a tooltip tip = Balloon(win) #Create a Button widget my_button=Button(win, text= "Hover Me") my_button.pack(pady=20) #Bind the tooltip with button tip.bind_widget(my_button, balloonmsg="www.tutorialspoint.com") win.mainloop()OutputThe above code will display the following window with a button "Hover Me". When the user hovers ... Read More

How do I handle the window close event in Tkinter?

Dev Prakash Sharma
Updated on 27-Mar-2021 06:09:56

5K+ Views

Tkinter provides a custom handler to close the window. It acts as a callback function that the user can run in order to close the window.To close the window using the handler, we can use the destroy() method. It closes the window abruptly after calling it in any function or any widget. Let us invoke the close event handler by defining a method.By using as an argument in WidgetExample#Importing the required library from tkinter import * #Create an instance of tkinter frame or window win= Tk() #Set the geometry win.geometry("600x400") #Create a button and pass arguments in ... Read More

How do I insert a JPEG image into a Python Tkinter window?

Dev Prakash Sharma
Updated on 27-Mar-2021 06:08:27

2K+ Views

Python provides the Pillow (PIL) package to support, process, and display the images in tkinter applications. A Tkinter application generally supports image files such as, ppm, png, and gif.Let us suppose we want to embed and display a JPEG or JPG image in our application.Tkinter Label widgets are generally used to display the text or images on the window and thus by passing the img value, we can display the JPEG image in the window.Example#Import required libraries from tkinter import * from PIL import ImageTk, Image #Create an instance of tkinter window win =Tk() #Define the geometry of ... Read More

How do I set a minimum window size in Tkinter?

Dev Prakash Sharma
Updated on 27-Mar-2021 06:08:00

2K+ Views

A Tkinter window can be initialized after running the application. Generally, the width and the height of the window is resizable which can be minimized.In order to set the window size to its minimum value, assign the value of width and height in minsize(height, width) method. The method can be invoked with the window or frame object.Example#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Minimize the window win.minsize(150, 100) #Create a text label Label(win, text= "Window Size is minimized to 150x100", font=('Helvetica bold', 20)).pack(pady=20) win.mainloop()OutputRunning the above code ... Read More

How to attach a Scrollbar to a Text widget in Tkinter?

Dev Prakash Sharma
Updated on 27-Mar-2021 06:07:30

2K+ Views

Tkinter Text widget is used to accept multiline user Input. It is similar to Entry Widget but the only difference is that Text widget supports multiple line texts. In order to create a Text widget, we have to instantiate a text object.Adding multiple texts will require to add the ScrollBar. In order to add a scrollbar in the text widget, we can call the ScrolledText(root) function. This function generally creates a text field with a scrollbar.The ScrolledText(root) function resides in Tkinter ScrolledText Module. We can import it using the following command, from tkinter.scrolledtext import ScrolledTextExampleIn this example, we will create ... Read More

How to bundle a Python Tkinter application including dependencies?

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

745 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

How to center a window on the screen in Tkinter?

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

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

How to change a Tkinter widget's font style without knowing the widget's font family/size?

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

212 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

How to change the color of certain words in a Tkinter text widget?

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

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

Advertisements