Found 26504 Articles for Server Side Programming

How to change the color of ttk button in Tkinter?

Dev Prakash Sharma
Updated on 15-Apr-2021 14:08:57

6K+ Views

Tkinter widgets have a consistent look and style across all the platforms and operating systems. Ttk works like CSS in an HTML script. It has many inbuilt functions, modules and methods that add style to a regular tkinter widget. Tkinter ttk buttons generally have a default color scheme, thus we can change the background color of these buttons by configuration method.ExampleIn this example, we will create a button which when pressed will change its style.#Import the necessary library import itertools from tkinter import * from tkinter import ttk #Create an instance of tkinter window win = Tk() #Set the geometry ... Read More

How to 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

How to bind a click event to a 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

How to align text to the left in Tkinter Label?

Dev Prakash Sharma
Updated on 22-Oct-2023 03:10:55

28K+ Views

Tkinter Label widget can be aligned using the anchor attributes. In order to calculate the accommodate spacing and alignment of the widget, anchor would help in a better way. Anchor provides several options such as N, W, S, E, NW, NE. SW, SE which can be defined in the pack manager itself.ExampleIn the following example, we will align the Label text of an application to the left by adding the anchor attribute towards “w” direction.#Import the required library from tkinter import* #Create an instance of tkinter frame win= Tk() #Set the geometry win.geometry("750x250") #Create a Label Widget Label(win, text= "New ... Read More

How to add text inside a Tkinter Canvas?

Dev Prakash Sharma
Updated on 14-Sep-2023 13:26:40

46K+ Views

Canvas is undoubtedly one of the most versatile widgets in Tkinter. With Canvas, we can create shapes, texts, animate stuff, modeling 3D shapes, modeling simulations, and many more.In order to add text inside a tkinter frame, we can use the create_text() method. We can define create_text() by adding values of font, text, and other options such as create_text(x, y, font, text, options….).Example#Import the required library from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry win.geometry("750x280") #Create a canvas object canvas= Canvas(win, width= 1000, height= 750, bg="SpringGreen2") #Add a text in ... Read More

How to add space between two widgets placed 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

How do I 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

How do I get the 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

How do I get the '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

Python program to copy all elements of one array into another array

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

Advertisements