Change Text Cursor Color in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 14:07:01

2K+ Views

Tkinter Entry and text widgets are used to create single and multiline text input fields. In order to change the color of the cursor, we can specify the insertbackground property by assigning the color of the cursor.ExampleIn this example, we have created the text field and we have changed the color of the cursor by defining the insertbackground property.#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win= Tk() win.geometry("750x250") #Create a text field text= Text(win) text.configure(bg= 'SteelBlue3', insertbackground='red') text.pack() win.mainloop()OutputNow, write something in the text field to see the reflected color of the cursor.Read More

Bind Click Event to Canvas in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 14:04:40

3K+ Views

The Canvas widget is undoubtedly the most powerful widget available in tkinter. It can be used to create and develop from custom widgets to a complete user interface. We can even bind the click event to handle the canvas and its object.ExampleIn this example, we will add an image inside the canvas widget and will bind a button object to remove the image from the canvas.In order to bind a click event, we can use the tag_bind() method and use the delete(image object) to delete the image.#Import the required library from tkinter import* #Create an instance of tkinter frame win= ... Read More

Add Space Between Two Widgets in a Grid in Tkinter

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

10K+ Views

Let us assume that we are creating a tkinter application where two or more widgets are placed using a grid property. We have to add some space between the widgets in order to style their appearance. To provide space in the widgets, we can use padding property, as padding adds space to the outermost part of the widget. In order to add padding, assign the values to padx and pady.Example#Import the required library from tkinter import * #create an instance of tkinter frame win= Tk() win.geometry("750x250") #Create some Button widgets Label(win, text= "New Line Text", font= ('Helvetica 20 bold')).grid(row=0, column=5, ... Read More

Give Focus to a Python Tkinter Text Widget

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

3K+ 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 widgets: an Entry widget and a text widget in the same window. By using the focus_set() method, we will activate the focus on the text widget.#Import tkinter library from tkinter import * from PIL import Image, ImageTk ... Read More

Copy All Elements of One Array into Another Array in Python

AmitDiwan
Updated on 15-Apr-2021 13:48:39

1K+ Views

When it is required to copy all the elements from one array to another, an empty array with ‘None’ elements is created. A simple for loop is used to iterate over the elements, and the ‘=’ operator is used to assign values to the new list.Below is a demonstration of the same −Example Live Demomy_list_1 = [34, 56, 78, 90, 11, 23] my_list_2 = [None] * len(my_list_1) for i in range(0, len(my_list_1)):    my_list_2[i] = my_list_1[i] print("The list is : ") for i in range(0, len(my_list_1)): print(my_list_1[i]) print() print("The new list : ") for i in ... Read More

Construct a Tree and Perform Insertion, Deletion, Display in Python

AmitDiwan
Updated on 15-Apr-2021 13:47:54

630 Views

When it is required to construct a binary tree, and perform operations such as inserting an element, deleting an element and displaying elements of the tree, a class is defined with methods in it. An instance of the class is defined and this is used to access the elements and perform operations.Below is a demonstration of the same −Example Live Democlass Tree_struct:    def __init__(self, data=None, parent=None):       self.key = data       self.children = []       self.parent = parent    def set_root(self, data):       self.key = data    def add_node(self, node): ... Read More

Get Width and Height of a Tkinter Window

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

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

208 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

Advertisements