Found 605 Articles for Tkinter

How can I display an image using Pillow in Tkinter?

Dev Prakash Sharma
Updated on 15-Apr-2021 13:35:40

1K+ Views

Python provides Pillow Package (PIL) to process and load the images in the application. An image can be loaded using the inbuilt Image.open("image location") method. Further, we can use a Label widget to display the Image in the window.Example#Import tkinter library from tkinter import * from PIL import Image, ImageTk #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("750x550") #Load the image img= Image.open("tutorialspoint.jpg") #Convert To photoimage tkimage= ImageTk.PhotoImage(img) #Display the Image label=Label(win, image=tkimage) label.pack() win.mainloop()OutputRunning the above code will display an image in the window.Before executing the code, make sure you have the image in ... Read More

How can I disable typing in a ttk.Combobox tkinter?

Dev Prakash Sharma
Updated on 15-Apr-2021 13:33:03

2K+ Views

The ttk.Combobox is used to create dropdown menus in the Entry Widgets. To create the options, we just simply pass the strings to the values object of combobox. We can disable the combobox by passing the state as “readonly”.ExampleIn the following example, we will create a combobox whose state is disabled.#Import tkinter library from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter window win.geometry("750x250") #Create an instance of StringVar var= StringVar() #Create an Label Label(win, text="Select any Language", font= ('Helvetica 15 bold')).pack(pady=20) #Create Object of Tkinter Combobox ... Read More

Expand Text widget to fill the entire parent Frame in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 13:31:30

2K+ Views

Tkinter text widgets are generally used to create text fields that support multi-line user Input. Let us suppose that we have to resize the text widget that is defined in a separate frame. To enable the text widget to resize in its full screen, we can use the columnand row configuration property of the grid system.We will use the grid_columnconfigure() property. It has four valid options like, Minsize − To provide the minimum size to the screen permitted in the application.Weight − Adds space to the widget in the layout.uniform − Place the column in a uniform group with other ... Read More

Getting every child widget of a Tkinter window

Dev Prakash Sharma
Updated on 15-Apr-2021 13:28:18

9K+ Views

In order to get the information about the widgets, tkinter provides a set of methods that can be used to test the widgets in an application. In order to get the list of all the child widgets, we can use the winfo_children() method.ExampleIn this example, we have defined some widgets in a frame and by using the winfo_children() method, we will print the list containing the names of all the widgets.#Import tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("750x200") #Create a frame frame= Frame(win) #Create label, button widgets Label(frame, ... Read More

Disable the underlying window when a popup is created in Python TKinter

Dev Prakash Sharma
Updated on 15-Apr-2021 13:26:46

2K+ Views

We are familiar with popup and used it in many applications. Popup in a tkinter application can be created by creating an instance of Toplevel(root) window. For a particular application, we can trigger the popup on a button object.Let us create a Python script to close the underlying or the main window after displaying the popup. We can close the main window while residing in a popup window by using the withdraw() method.ExampleThrough this example, we will create a popup dialog that can be triggered after clicking a button. Once the popup will open, the parent window will close automatically.#Import ... Read More

Difference between import tkinter as tk and from tkinter import

Dev Prakash Sharma
Updated on 15-Apr-2021 13:23:42

4K+ Views

In order to work with tkinter applications and widgets, we have to import the tkinter library in the environment. There are multiple ways to import the tkinter library in the notebook.Using from tkinter import *Using import tkinter as tkThe first method to import the tkinter library is the most common, as it comes with all the inbuilt methods or functions. In the general sense, we don’t have to explicitly override the method for the widgets. In this way, we can create the object of widgets just by using the widget constructor. It comes with all the modules defined in tkinter.However, ... Read More

Difference between "fill" and "expand" options for tkinter pack method

Dev Prakash Sharma
Updated on 15-Apr-2021 13:19:17

3K+ Views

Tkinter Pack Manager acts based on using the extra space within the parent widget. While packing a widget, we can specify whether the widget should shrink or fill in the entire screen. In order to enable the widget to grow in the entire window, we can use the 'fill' property. It helps to fill the widget in the screen by adding “x” as horizontal, “y” as vertical or “BOTH”.Whenever we use expand(boolean) property, then we are generally resizing the widget size to expand in its available space. It takes boolean values as true or false. When the expand property is ... Read More

Creating a popup message box with an Entry field in tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 13:17:44

5K+ Views

Tkinter Popup are toplevel window interfaces that wrap up the widget and the element with the main window. It can be embedded in any main window using a handler like Button Widget. Popup can be created using the Toplevel(root) constructor.ExampleIn this example, we will create a simple application containing a Label widget and a button to open the popup message box which contains an Entry field. Once the popup is opened, it can have the functionality to go back to the main window.#Import the required library from tkinter import* #Create an instance of tkinter frame win= Tk() #Define geometry of ... Read More

Change the focus from one Text widget to another in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 13:13:45

686 Views

In various applications, tkinter widgets are required to be focused to make them active. Widgets can also grab focus and prevent the other events outside the bounds. To manage and give focus to a particular widget, we generally use the focus_set() method. It focuses the widget and makes them active until the termination of the program.ExampleIn the following example, we have created two text widgets and we will change the focus from one text widget to another text widget simultaneously using a button widget. Thus, changing the focus is easy by defining two methods which can be handled through the ... Read More

Change command Method for Tkinter Button in Python

Dev Prakash Sharma
Updated on 15-Apr-2021 13:11:16

4K+ Views

The significance of Button widget is that it is used for handling events to perform certain operations in the application. In order to handle such events, we generally define a method which contains certain operations.Let us suppose we want to change the event method after initializing the button. We can configure the Button and its handler using configure(options) method. Thus, by defining a new method and configuring the button we can trigger a new event with the same button.Example#Import tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("750x250") #Define ... Read More

Advertisements