Click Here
Download PythonTkinter is included with the standard Python distribution. To verify Tkinter is installed, you can try importing it in a Python shell:
import tkinter as tk
If this command runs without error, Tkinter is installed and ready to use.
Let's create a simple window using Tkinter.
import tkinter as tk # Create the main window (root window) root = tk.Tk() # Set the window title root.title("Hello Tkinter") # Set the window size root.geometry("400x300") # Run the application root.mainloop()
tk.Tk()
: Initializes the main window.root.title("Hello Tkinter")
: Sets the title of the window.root.geometry("400x300")
: Sets the size of the window (width x height).root.mainloop()
: Starts the event loop, waiting for user interactions.Example with a Label and a Button:
import tkinter as tk def on_button_click(): label.config(text="Button Clicked!") # Create the main window root = tk.Tk() root.title("Simple Tkinter App") root.geometry("300x200") # Create a Label label = tk.Label(root, text="Hello, Tkinter!") label.pack(pady=10) # Create a Button button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=10) # Run the application root.mainloop()
tk.Label
: Creates a label widget.label.pack()
: Packs the label into the window, managing its placement.tk.Button
: Creates a button widget.button.pack()
: Packs the button into the window.command=on_button_click
: Binds the button click event to the
on_button_click
function.Tkinter is a powerful and easy-to-use toolkit for creating GUI applications in Python. With just a few lines of code, you can create windows, handle user inputs, and manage the application’s interface. This introduction covers the basics to get you started with Tkinter. As you progress, you will learn more about the wide range of widgets and capabilities Tkinter offers.