Keep Window Focus on New Toplevel Window in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 09:11:27

7K+ Views

Tkinter toplevel class contains toplevel window which is a child window other than the main window. Whenever we create a toplevel window, it just appears above the main window along with the widgets defined in it.To keep the window toplevel window focused, we can use grab_set() method. It always keeps the toplevel window above all the other windows.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() win.geometry("700x350") def open_win():    top = Toplevel(win)    top.geometry("700x250")    Label(top, text= "Hey Folks!", font= ('Helvetica 14 bold')).pack()    top.grab_set() #Create a Label to print the ... Read More

Hide Console of EXE File Created with PyInstaller in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 08:56:17

12K+ Views

To convert a standard Tkinter application into a window executable file, we generally use thePyintsaller package. It converts the application file into an executable application. However, we notice that when we open the executable (or .exe) file, it displays a command shell before opening the application window. We can hide or avoid the console by specifying pyinstaller --oneline filename --windowed command.ExampleIn this example, we will create an .exe file of the following program using PyInstaller.app.py#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x350") #Set the ... Read More

Remove Icon from Title Bar in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 08:52:26

5K+ Views

To remove the default icon of the Tkinter window, we can use wm_attributes('type', 'value') method by specifying the type of property. In the following example, we will use '-toolwindow', a Boolean value that removes the icon associated with the title bar of the application.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() win.geometry("700x350") #Create a Label to print the Name label= Label(win, text="This is a New Line Text", font= ('Helvetica 14 bold'), foreground= "red3") label.pack() win.wm_attributes('-toolwindow', 'True') win.mainloop()OutputWhen we run the above code, it will display a Tkinter window ... Read More

Remove Title Bar in Tkinter Window without Overrideredirect Method

Dev Prakash Sharma
Updated on 25-May-2021 08:51:14

9K+ Views

To remove the title bar of a Tkinter window, we can use wm_attributes('type', 'value') method by specifying the type of property. In the following example, we will use 'fullscreen', a Boolean value that removes the title bar of the window.Example#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() win.geometry("700x350") #Create a Label to print the Name label= Label(win, text="This is a New Line Text", font= ('Helvetica 14 bold'), foreground= "red3") label.pack() win.wm_attributes('-fullscreen', 'True') win.mainloop()OutputRunning the above code will display a fullscreen window without the title bar.Read More

Difference Between Tkinter's Tk and Toplevel Classes

Dev Prakash Sharma
Updated on 25-May-2021 08:50:56

1K+ Views

Tkinter windows are created by initializing the Tk object first. It is the minimal part of any Tkinter application, which helps to instantiate the application. Tk helps to construct the basic building blocks of the application, such as an application window where all the widgets are placed.However,  Toplevel classes help to communicate through the internal widgets of the main application. One of the examples of toplevel classes is the Toplevel window that displays a child window other than the main window of the application. The Toplevel window works the same as the Tk, as it also can contain widgets and functionalities.Example#Import the required ... Read More

Default Window Color and Hex Color Codes in Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 08:50:36

2K+ Views

A Tkinter window can be customized by adding the properties and attributes such as background color, foreground color, width, height, etc.The color attribute in config() defines the default color of the main window. We can set the color of the window by defining either Hex Color (e.g., #000 for Black) or the Name of the color. Example# Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x350") ... Read More

Compile Python 3 App to EXE Using Tkinter

Dev Prakash Sharma
Updated on 25-May-2021 08:50:13

4K+ Views

Python is well-known for its rich library of extensions and packages. We can import and install the necessary packages from the library. However, if we require to run a Tkinter application with an executable file in Windows Operating System, then we can use the Pyinstaller package in Python. It converts a Python-based application to a native executable file (or.exe).Follow the steps to compile a Tkinter-based application into an executable file, Install Pyinstaller using 'pip install pyinstaller'.Open the Command or Shell in the same directory where the application file is located and run the file using the command, pyinstaller --onefile app.py. It ... Read More

Tkinter Dropdown Menu with Keyboard Shortcuts

Dev Prakash Sharma
Updated on 25-May-2021 08:49:46

1K+ Views

A Dropdown Menu is nothing but a list of vertically stacked menu items that can be visible at the top Menu Bar of an application. We can create a Menu bar in a Tkinter application by creating an object of Menu() in which all the Menu items are present.There might be a case when we want to select the menu and perform some basic operations using Keyboard shortcuts. In order to bind the key to all the Menu, we use the bind_all(, callback) method.ExampleIn this example, the application window contains a Menu of items. When we press the combination of , it ... Read More

Bind Events to Tkinter Canvas Items

Dev Prakash Sharma
Updated on 25-May-2021 08:49:19

2K+ Views

Tkinter events can be bound with the widgets to perform a set of operations on the widgets. To be more specific, we can also bind an event handler to Canvas Items by using bind(, callback) method. Binding the event with the canvas item makes a canvas item dynamic which can be customized by event handlers.Example#Import the required Libraries from tkinter import * import random #Create an instance of Tkinter frame win = Tk() #Set the geometry of the window win.geometry("700x350") #Crate a canvas canvas=Canvas(win, width=700, height=350, bg='white') def draw_shapes(e):    canvas.delete(ALL) canvas.create_oval(random.randint(5, 300), random.randint(1, 300), 25, 25, ... Read More

Specify Where a Tkinter Window Should Open

Dev Prakash Sharma
Updated on 25-May-2021 08:47:17

10K+ Views

Tkinter window can be configured using the Geometry Manager. When we specify the main window using the geometry(width x height + position_right + position_left) method, then we generally enable the window to open in a particular position.Example#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x350+300+300") #Create a Label Label(win, text="This Window Opens at (300,300)", font=('Helvetica 15 bold')).pack(pady=30) win.mainloop()OutputRunning the above code will display a window at the specified position with a label text.

Advertisements