import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Hello, World!", font=("Arial", 12)) label.pack() print(label.cget("text"))
import tkinter as tk root = tk.Tk() label = tk.Label(root, text="0") label.pack() def increment(): current_value = int(label.cget("text")) new_value = current_value + 1 label.config(text=str(new_value)) button = tk.Button(root, text="Increment", command=increment) button.pack() root.mainloop()Here, we create a label widget with an initial value of 0 and a button that when clicked, increments the value displayed on the label by 1. The cget method is used to retrieve the current value of the text option on the label, which is then incremented and set back as the new value. The package library for tkinter, the GUI toolkit used in these examples, is included in the standard Python library.