Using A Frame Class in Tkinter

Dev Prakash Sharma
Updated on 05-Aug-2021 13:59:25

2K+ Views

Tkinter Frame widget is very useful for grouping multiple widgets in a frame. It includes all the functions and properties that applies to the parent window.To create a Frame widget, we can instantiate an object of the Frame class. Once we define the Frame widget in the window, we can directly pick any widget and place it into the frame.ExampleIn this example, we've created a Frame widget and defined some widgets in it.# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the ... Read More

Bind Enter Key to Tkinter Window

Dev Prakash Sharma
Updated on 05-Aug-2021 13:57:57

20K+ Views

Tkinter events are executed at runtime and when we bind these events with a button or key, then we will get access to prioritize the event in the application.To bind the key with an event in Tkinter window, we can use bind('', callback) by specifying the key and the callback function as the arguments. Once we bind the key to an event, we can get full control over the events.Example# Import the required libraries from tkinter import * from PIL import Image, ImageTk # Create an instance of tkinter frame or window win=Tk() # Set the size ... Read More

Draw PNG Image on Python Tkinter Canvas

Dev Prakash Sharma
Updated on 05-Aug-2021 13:56:10

3K+ Views

To work with images in tkinter, Python provides PIL or Pillow toolkit. It has many built-in functions that can be used to operate an image of different formats.To open an image in a canvas widget, we have use create_image(x, y, image, **options) constructor. When we pass the Image value to the constructor, it will display the image in the canvas.Example# Import the required libraries from tkinter import * from PIL import Image, ImageTk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x600") # Create a canvas widget canvas=Canvas(win, ... Read More

Set Font Size of Entry Widget in Tkinter

Dev Prakash Sharma
Updated on 05-Aug-2021 13:54:30

5K+ Views

The Entry widget in tkinter is a basic one-line character Entry box that accepts single line user input. To configure the properties of the Entry widget such as its font-size and width, we can define an inline widget constructor.ExampleHere is an example of how you can define the font-size of the Entry widget.# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Create an Entry widget entry=Entry(win, width=35, font=('Georgia 20')) entry.pack() win.mainloop()OutputRun the above ... Read More

Change Width of a Frame Dynamically in Tkinter

Dev Prakash Sharma
Updated on 05-Aug-2021 13:53:31

25K+ Views

The frame widget in Tkinter works like a container where we can place widgets and all the other GUI components. To change the frame width dynamically, we can use the configure() method and define the width property in it.ExampleIn this example, we have created a button that is packed inside the main window and whenever we click the button, it will update the width of the frame.# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") ... Read More

Dynamically Add, Remove, and Update Labels in a Tkinter Window

Dev Prakash Sharma
Updated on 05-Aug-2021 13:51:35

15K+ Views

We can use the Tkinter Label widget to display text and images. By configuring the label widget, we can dynamically change the text, images, and other properties of the widget.To dynamically update the Label widget, we can use either config(**options) or an inline configuration method such as for updating the text, we can use Label["text"]=text; for removing the label widget, we can use pack_forget() method.Example# Import the required libraries from tkinter import * from tkinter import ttk from PIL import ImageTk, Image # Create an instance of tkinter frame or window win=Tk() # Set the size of the ... Read More

Draw a Scale Ranging from Red to Green in Tkinter

Dev Prakash Sharma
Updated on 05-Aug-2021 13:49:31

2K+ Views

A color gradient defines the range of position-dependent colors. To be more specific, if you want to create a rectangular scale in an application that contains some color ranges in it (gradient), then we can follow these steps −Create a rectangle with a canvas widget and define its width and height.Define a function to fill the color in the range. To fill the color, we can use hex values inside a tuple.Iterate over the range of the color and fill the rectangle with it.Example# Import the required libraries from tkinter import * from tkinter import ttk # Create an ... Read More

Get RadioButton Output in Tkinter

Dev Prakash Sharma
Updated on 05-Aug-2021 13:47:19

11K+ Views

The radiobutton widget in Tkinter allows the user to make a selection for only one option from a set of given choices. The radiobutton has only two values, either True or False.If we want to get the output to check which option the user has selected, then we can use the get() method. It returns the object that is defined as the variable. We can display the selection in a label widget by casting the integer value in a string object and pass it in the text attributes.Example# Import the required libraries from tkinter import * from tkinter import ttk # ... Read More

Add Second X-Axis at Bottom in Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 13:00:22

1K+ Views

To add a second X-axis at the bottom of the first one in Matplotlib, we can take the followingStepsSet the figure size and adjust the padding between and around the subplots.Get the current axis (ax1) using gca() method.Create a twin axis (ax2) sharing the Y-axis.Set X-axis ticks at AxisSet X-axis labels at Axis 1 andTo display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ax1 = plt.gca() ax2 = ax1.twiny() ax2.set_xticks([1, 2, 3, 4, 5]) ax1.set_xlabel("X-axis 1") ax2.set_xlabel("X-axis 2") plt.show()Output

Create a Swarm Plot with Matplotlib

Rishikesh Kumar Rishi
Updated on 04-Aug-2021 12:55:54

1K+ Views

To create a Swarm Plot with Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe, i.e., a two-dimensional, size-mutable, potentially heterogeneous tabular data.Initialize the plotter, swarmplot.To plot the boxplot, use boxplot() method.To display the figure, use show() method.Exampleimport seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = pd.DataFrame({"Box1": np.arange(10),                      "Box2": np.arange(10)}) ax = sns.swarmplot(x="Box1", y="Box2", data=data, zorder=0) ... Read More

Advertisements