Found 604 Articles for Tkinter

How do I position the buttons on a Tkinter window?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:38:26

10K+ Views

To set the position of a button, we use the place method of button widget. The place method takes the x and y coordinates of the button.Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using win.geometry method.Next, create multiple buttons and name them "Button-1", "Button-2", etc.Set the position of the buttons using the place method by supplying the x and y coordinate values.Finally, run the mainloop of the application window.Example# Import the Tkinter library from tkinter import * from tkinter import ttk # Create an instance of Tkinter frame win ... Read More

How to bind a Tkinter event to the left mouse button being held down?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:35:43

3K+ Views

To bind a Tkinter event to the left mouse button being held down, we can take the following steps −Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define an event handler "handler1" to print a statement when the mouse is moved with the left button being held down.Define another event handler "handler2" to print a statement when the mouse button is released.Use the bind method to bind with handler1.Use the bind method again to bind with hander2.Finally, run the mainloop of the application window.Example# Import required libraries from tkinter import * # ... Read More

How to save the contents of a Textbox in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:32:06

6K+ Views

To save the contents of a Textbox in Tkinter, we can take the following steps −Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define a user-defined method "open_text" to open a text file in "read" mode. Read the contents of the text file and save it in a variable called "content". Then, use the "insert" method to insert the contentin a Textbox.Next, define another user-defined method called "save_text" and in it, use the "write" method to save the contents of the textbox in the text file.Create a text widget using the Text method with specified ... Read More

How to make a new folder using askdirectory dialog in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:25:13

2K+ Views

To make a new folder using askdirectory dialog in Tkinter, we can take the following steps −Import the required modules. filedialog module is required for askdirectory method. os module is required for makedirs method.Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define a user-defined method "create_subfolder". Inside the method, call filedialog.askdirectory to select a folder and save the path in a variable, source_path.We can use askdirectory method of filedialog to open a directory. Save the path of the selected directory in a 'path' variable.Then, use os.path.join and makedirs to create a sub-folder inside the parent ... Read More

How to change the size of text on a label in Tkinter?

Dev Prakash Sharma
Updated on 06-Aug-2021 07:49:05

21K+ Views

The label widget in Tkinter is used to display text and images in a Tkinter application. In order to change the properties of the label widget such as its font-property, color, background color, foreground color, etc., you can use the configure() method.If you want to change the size of the text in a Label widget, then you can configure the font=('font-family font-size style') property in the widget constructor.Example# Import the required libraries from tkinter import * import tkinter.font as tkFont # Create an instance of tkinter frame or window win=Tk() # Set the size of the tkinter window ... Read More

How to use the Entry widget in Tkinter?

Dev Prakash Sharma
Updated on 06-Aug-2021 07:44:00

2K+ Views

The Entry widget is a single-line text widget defined in Tcl/Tk toolkit. We can use the Entry widget to accept and display single-line user input.In order to use the Entry widget, you have to first create an Entry widget using the constructor Entry(parent, width, **options). Once we've defined our Entry widget, we can configure its properties such as font-properties, color, width, etc., using the configure() method.ExampleLet use create an Entry widget to accept the username and display it in the window.# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win ... Read More

How to create a Tkinter error message box?

Dev Prakash Sharma
Updated on 06-Aug-2021 07:40:41

16K+ Views

The Tkinter library has many built-in functions and methods which can be used to implement the functional part of an application. We can use messagebox module in Tkinter to create various popup dialog boxes. The messagebox property has different types of built-in popup windows that the users can use in their applications.If you need to display the error messagebox in your application, you can use showerror("Title", "Error Message") method. This method can be invoked with the messagebox itself.Example# Import the required libraries from tkinter import * from tkinter import messagebox # Create an instance of tkinter frame or window ... Read More

How to change Tkinter label text on button press?

Dev Prakash Sharma
Updated on 06-Aug-2021 07:29:13

9K+ Views

Most often, Tkinter Label widgets are used in the application to display the text or images. We can configure the label widget such as its text property, color, background or foreground color using the config(**options) method.If you need to modify or change the label widget dynamically, then you can use a button and a function to change the text of the label widget.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size of the tkinter window win.geometry("700x350") # Define a function update ... Read More

How to reconfigure Tkinter canvas items?

Dev Prakash Sharma
Updated on 06-Aug-2021 06:45:03

8K+ Views

Using the Canvas widget, we can create text, images, graphics, and visual content to add to the Canvas widget. If you need to configure the Canvas item dynamically, then tkinter provides itemconfig(**options) method. You can use this method to configure the properties and attributes of the Canvas items. For example, if we create a line inside the Canvas widget, we can configure its color or width using itemconfig() method.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size of the tkinter window win.geometry("700x350") ... Read More

How to set justification on Tkinter Text box?

Dev Prakash Sharma
Updated on 06-Aug-2021 06:40:30

3K+ Views

The Text widget supports multiline user input from the user. We can configure the Text widget properties such as its font properties, text color, background, etc., by using the configure() method.To set the justification of our text inside the Text widget, we can use tag_add() and tag_configure() properties. We will specify the value of "justify" as CENTER.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win=Tk() # Set the size of the tkinter window win.geometry("700x350") # Create a text widget text=Text(win, width=40, height=10) # justify the text ... Read More

Advertisements