
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
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()
Output
Executing the above code will display a window with a button and a text label showing the button text.
- Related Articles
- 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?
- How to highlight text in a tkinter Text widget?
- How to set the text/value/content of an 'Entry' widget using a button in Tkinter?
- How do I center the text in a Tkinter Text widget?
- Underline Text in Tkinter Label widget
- Get contents of a Tkinter Entry widget
- How to clear the contents of a Tkinter Text widget?
- How to make a Button using the Tkinter Canvas widget?
- Printing a list to a Tkinter Text widget
- How to highlight the current line of a Text widget in Tkinter?
- Undo and redo features in a Tkinter Text widget
- How to create hyperlink in a Tkinter Text widget?

Advertisements