- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the width of the Tkinter widget?
Tkinter widgets are supposed to be present in the Tkinter application window. All the widgets can be configured and customized by using predefined properties or functions.
To get the width of a widget in a Tkinter application, we can use winfo_width() method. It returns the width of the widget which can be printed later as the output.
#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x350") #Set the default color of the window win.config(bg='#aad5df') #Create a Label to display the text label=Label(win, text= "Hello World!",font= ('Helvetica 18 bold'), background= 'white', foreground='purple1') label.pack(pady = 50) win.update() #Return and print the width of label widget width = label.winfo_width() print("The width of the label is:", width, "pixels") win.mainloop()
Running the above code will display a window that contains a Label widget.
When we compile the code, it will print the width of the label widget on the console.
The width of the label is: 148 pixels
- Related Articles
- How to set the width of a Tkinter Entry widget in pixels?
- How to set the height/width of a Label widget in Tkinter?
- How to get the input from the Tkinter Text Widget?
- How to get the value of an Entry widget in Tkinter?
- How to get the widget name in the event in Tkinter?
- How to get rid of widget border in Tkinter?
- How to get the current length of the Text in a Tkinter Text widget?
- How to get the value of a button in the Entry widget using Tkinter?
- Get the text of a button widget in Tkinter
- How do I get the background color of a Tkinter Canvas widget?
- Get contents of a Tkinter Entry widget
- How to get the Tkinter widget's current x and y coordinates?
- How to use the Entry widget in Tkinter?
- How do I get the width and height of a Tkinter window?
- How to clear the contents of a Tkinter Text widget?