Tkinter




Python Tkinter Tutorial

Python Tkinter Tutorial

Tkinter is a Python GUI (Graphical User Interface) toolkit that is used to create software applications with a graphical interface.

Why use Tkinter?

Tkinter is one of the most commonly used GUI toolkits in the Python world. Some of the reasons why programmers choose Tkinter include:

  • Easy to use
  • Compatible with both Python 2 and Python 3
  • Cross-platform
  • Customizable look and feel
  • Built-in widgets and tools

With Tkinter, you can create a variety of GUI elements, including:

  1. Buttons
  2. Labels
  3. Text boxes
  4. Radio buttons
  5. Checkboxes
  6. Menus
  7. List boxes
  8. Scrollbars
  9. Canvas
  10. And more!

Getting Started with Tkinter

To use Tkinter, you need to import the module:

	import tkinter as tk

Once you have imported Tkinter, you can create a new window or frame:

	root = tk.Tk()

This creates a new instance of the Tk class, which is the main window or frame for your application.

After creating the new window or frame, you can add widgets to it:

	label = tk.Label(root, text="Hello, world!")
	label.pack()

This creates a new label widget with the text “Hello, world!” and adds it to the main window or frame using the pack() method.

Widget Properties and Methods

Each Tkinter widget has its own set of properties and methods that can be used to customize its appearance and behavior.

Properties

Some of the most commonly used widget properties include:

	bg - background color
	fg - foreground color
	font - font family and size
	height - widget height
	width - widget width
	text - widget text
	image - widget image
	

You can set these properties when creating a new widget:

	label = tk.Label(root, text="Hello, world!", fg="red", font=("Helvetica", 16))
	button = tk.Button(root, text="Click me!", bg="blue", fg="white", height=2, width=8)

Methods

Widgets also have methods that can be used to perform actions or change their state:

	config - change widget properties
	cget - get widget properties
	pack - add widget to parent
	grid - add widget to grid
	destroy - remove widget
	

You can call these methods on a widget to customize its behavior:

	label.config(text="Goodbye, world!", fg="blue")
	button.destroy()

Layout Management

Tkinter provides two main layout managers: pack and grid.

The pack() method is used to add widgets to a container one after the other. By default, widgets are added to the top of the container:

	label1 = tk.Label(root, text="Label 1", bg="red")
	label1.pack()
	label2 = tk.Label(root, text="Label 2", bg="green")
	label2.pack()
	label3 = tk.Label(root, text="Label 3", bg="blue")
	label3.pack()

This will create a vertical stack of labels with different background colors.

The grid() method is used to add widgets to a container in a grid-like fashion:

	label1 = tk.Label(root, text="Label 1", bg="red")
	label1.grid(row=0, column=0)
	label2 = tk.Label(root, text="Label 2", bg="green")
	label2.grid(row=0, column=1)
	label3 = tk.Label(root, text="Label 3", bg="blue")
	label3.grid(row=1, column=0, columnspan=2)

This will create a grid of labels with different background colors.

Event Handling

Tkinter allows you to handle events such as mouse clicks, keyboard presses, and window resizing.

To handle an event, you need to define a function that will be called when the event occurs:

	def button_pressed():
	    print("Button was pressed!")

You can then attach the function to a widget and specify which event to handle:

	button = tk.Button(root, text="Click me!")
	button.pack()
	button.bind("", button_pressed)

This will create a new button widget and attach the button_pressed() function to the left mouse button click event.

Conclusion

Tkinter is a powerful and easy-to-use GUI toolkit for Python that allows you to create software applications with a graphical interface. With Tkinter, you can create a wide range of GUI elements, customize their appearance and behavior, and handle user events.


Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top