Get Width and Height of a Tkinter Window

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

22K+ Views

Tkinter initially creates an instance of a window which is a container that contains all the widgets and the elements. If we want to resize the window, we can use the geometry method by defining the value of width and height.In order to get the width and height of the tkinter window, we can use winfo_width() and winfo_height() helper methods that help to grab the current width and height of the tkinter window.Example# Import tkinter library from tkinter import * # Create an instance of tkinter frame win = Tk() # Set the Geometry win.geometry("750x250") def print_width():    print("The width ... Read More

Check Consecutive Zeros in a Number using Python

AmitDiwan
Updated on 15-Apr-2021 13:44:27

220 Views

When it is required to check if a number has consecutive zeroes of a specific base, a method is defined, that takes the number and base as parameters, and uses another method to return Yes or No depending on whether the base is present or not.Below is a demonstration of the same −Example Live Demodef check_consecutive_zero(N, K):    my_result = convert_to_base(N, K)    if (check_n(my_result)):       print("Yes")    else:       print("No") def convert_to_base(N, K):    weight = 1    s = 0    while (N != 0):       r = N % K   ... Read More

Implement Queue in Python

AmitDiwan
Updated on 15-Apr-2021 13:43:53

1K+ Views

When it is required to implement a queue using Python, a queue class is created, and methods to add and delete elements are defined. An instance of the class is created, and these methods are called using the instance and relevant output is displayed.Below is a demonstration of the same −Example Live Democlass Queue_struct:    def __init__(self):       self.items = []    def check_empty(self):       return self.items == []    def enqueue_elem(self, data):       self.items.append(data)    def dequeue_elem(self):       return self.items.pop(0) my_instance = Queue_struct() while True:    print('Enqueue ') ... Read More

Python Program to Implement a Stack

AmitDiwan
Updated on 15-Apr-2021 13:43:28

4K+ Views

When it is required to implement a stack using Python, a stack class is created, and an instance of this class is created. Methods to push, pop elements are defined and the instance is used to call these methods.Below is a demonstration of the same −Example Live Democlass Stack_struct:    def __init__(self):       self.items = []    def check_empty(self):       return self.items == []    def add_elements(self, my_data):       self.items.append(my_data)    def delete_elements(self):       return self.items.pop() my_instance = Stack_struct() while True:    print('Push ')    print('Pop')    print('Quit')   ... Read More

Get State of a Tkinter Checkbutton

Dev Prakash Sharma
Updated on 15-Apr-2021 13:43:25

3K+ Views

Tkinter provides a variety of input widgets such as entry widget, text widget, listbox, combobox, spinbox, checkbox, etc. Checkboxes are used for taking validity input and the state gets active whenever the user clicks on the checkbutton. In terms of a particular application, we can check the state of the tkinter CheckButtons by using the state() method. It prints the actual state of the tkinter checkbuttons.Example#Import the required library from tkinter import* from tkinter import ttk #Create an instance of tkinter frame win= Tk() #Set the geometry win.geometry("750x250") #Define geometry of the window win.geometry("750x250") #Create CheckButtons chk= ttk.Checkbutton(win, text="Python") chk.pack() ... Read More

Create Child Windows with Python Tkinter

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

7K+ Views

The child window can be referred to as the independent window which is separated from the root or main window. In order to create a child window, we have to define a toplevel window which can be created manually using the Toplevel(win) method. In the method toplevel(root), we have to pass the main window as the parameter and further define the widgets if needed.ExampleLet us create a child window which contains some widgets in it.#Import tkinter library from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win = Tk() #Set the geometry and title of ... Read More

Create a Date Picker in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 13:39:25

9K+ Views

Tkcalendar is a Python package which provides DateEntry and Calendar widgets for tkinter applications. In this article, we will create a date picker with the help of DateEntry Widget.A DateEntry widget contains three fields that refer to the general format of Date as MM/DD/YY. By creating an object of DateEntry widget, we can choose a specific Date in the application.Example#Import tkinter library from tkinter import * from tkcalendar import Calendar, DateEntry #Create an instance of tkinter frame win= Tk() #Set the Geometry win.geometry("750x250") win.title("Date Picker") #Create a Label Label(win, text= "Choose a Date", background= 'gray61', foreground="white").pack(padx=20, pady=20) #Create a Calendar ... Read More

Pass Arguments to Tkinter Button's Callback Command

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

2K+ Views

Tkinter Buttons are used for handling certain operations in the application. In order to handle such events, we generally pass the defined function name as the value in the callback command. For a particular event, we can also pass the argument to the function in the button’s command.There are two ways to pass the argument to the tkinter button command −Using Lambda or anonymous functionUsing PartialsExampleIn this example, we will create a simple application that will contain a text label and a button to change the value of label text. We will pass the label as the argument in the ... Read More

Build Binary Tree from Inorder or Postorder Traversal in Python

AmitDiwan
Updated on 15-Apr-2021 13:35:40

429 Views

When it is required to build a binary tree by taking input using inorder or postorder traversal, a class is defined, that has methods to set the root element, perform inorder traversal, perform post order traversal. It can be used by creating an instance of the class.Below is a demonstration of the same −Example Live Democlass BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def inorder_traversal(self):       if self.left is not ... Read More

Display 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

Advertisements