import tkinter as tk root = tk.Tk() frame = tk.Frame(root, bg='red') frame.pack() color = frame.cget('bg') print(color) root.mainloop()
import tkinter as tk root = tk.Tk() label = tk.Label(root, text='Hello, world!') label.pack() text = label.cget('text') print(text) root.mainloop()In this example, we create a label widget with the text 'Hello, world!'. We then use the cget() function to get the current text of the widget and print it to the console. The output will be 'Hello, world!'. Package library: Tkinter Overall, the cget() function is a useful tool for getting the current value of an attribute of a widget in Tkinter. It can be used for a wide range of widgets and attributes, and is a key function in many Tkinter applications.