- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the text of a button widget in Tkinter
Let us suppose that for a particular application, we want to retrieve the button value by its name. In such cases, we can use the .cget() function. Every tkinter widget supports the .cget() function, as it can be used to retrieve the widget configuration such as value or name.
In this particular example, we will create a button and then store the button text in a variable "mytext". Using the variable, we will display the text in a Label widget.
#Import tkinter library from tkinter import * from tkinter import ttk #Create an instance of tkinter frame or window win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Create a button button= ttk.Button(win, text="My Button") button.pack() #Get the text of Button mytext= button.cget('text') #Create a label to print the button information Label(win, text=mytext, font= ('Helvetica 20 bold')).pack(pady=20) win.mainloop()
Executing the above code will display a window with a button and a text label showing the button text.
- 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?
- How to get the input from the Tkinter Text Widget?
- How to update a Button widget in Tkinter?
- Get contents of a Tkinter Entry widget
- How to highlight text in a tkinter Text widget?
- How to clear the contents of a Tkinter Text widget?
- How do I center the text in a Tkinter Text widget?
- Underline Text in Tkinter Label widget
- How to make a Button using the Tkinter Canvas widget?
- How to set the text/value/content of an 'Entry' widget using a button in Tkinter?
- Printing a list to a Tkinter Text widget
- How to get the width of the Tkinter widget?
- How to highlight the current line of a Text widget in Tkinter?
- Add advanced features to a tkinter Text widget