.get() Function for Button Widget in Tkinter
The .get()
method of the Button
widget in Tkinter is used to retrieve the current value of the button's text property.
Usage
button.get()
The following code sample shows you how to use the .get()
method:
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text="Click me!")
button.pack()
def on_click():
value = button.get()
print(value)
button.config(command=on_click)
window.mainloop()
Return Value
The .get()
method returns the current value of the button's text property as a string.
Example
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text="Click me!")
button.pack()
def on_click():
value = button.get()
print(value)
button.config(command=on_click)
window.mainloop()
When the button is clicked, the on_click()
function will be called. The on_click()
function will then use the .get()
method to retrieve the current value of the button's text property, which is "Click me!". The value will then be printed to the console.
Conclusion
The .get()
method is a useful method for retrieving the current value of a button's text property. This method can be used to create dynamic UIs that respond to user input.