import tkinter as tk
# Create window
window = tk.Tk()
window.title("My Program")
# Add label
label = tk.Label(window, text="Hello World")
label.pack()
# Start program
window.mainloop()
Explanation:
tk.Tk() → creates a new windowwindow.title() → sets the window titletk.Label → displays text inside the windowpack() → displays the elementwindow.mainloop() → starts the event loop; the window stays open
import tkinter as tk
def show_text():
label.config(text="Button was clicked!")
window = tk.Tk()
window.title("Button Example")
label = tk.Label(window, text="Hello World")
label.pack()
button = tk.Button(window, text="Click me!", command=show_text)
button.pack()
window.mainloop()