python logo


Tag: tk

tkinter button

The Tkinter library in Python makes it easy to develop interactive desktop applications. One of its versatile features is the ability to display buttons, serving as an interactive interface component. Here, we will explore how to create and customize buttons using this powerful library.

⚙️ Installation Check
Before proceeding, ensure that you’re using the right version of Python. Check your version:

1
python -- version

📘 Related Course:

Crafting Simple Buttons

Let’s begin by creating a basic button that prints a message upon clicking:

1
2
3
4
5
6
7
8
9
10
11
from Tkinter import *

master = Tk()

def callback():
print "Clicked!"

b = Button(master, text="Click Me", command=callback)
b.pack()

mainloop()

Tkinter Button

Crafting Image Buttons

Enhance your UI by using buttons with images. Here’s how:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from Tkinter import *

master = Tk()
master.minsize(300,100)
master.geometry("320x100")

def callback():
print "Clicked!"

photo = PhotoImage(file="add.png")
b = Button(master, image=photo, command=callback, height=50, width=150)
b.pack()

mainloop()

Image Button

Adding Text Labels to Image Buttons

For a blend of imagery and text, adjust your button like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from Tkinter import *

master = Tk()
master.minsize(300,100)
master.geometry("320x100")

def callback():
print "Clicked!"

photo = PhotoImage(file="add.png")
b = Button(master, image=photo, text="Click Me", command=callback, height=50, width=150, compound=LEFT)
b.pack()

mainloop()

Text and Image Button

Positioning Buttons on Your GUI

Take control of your button’s position using the place function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from Tkinter import *

master = Tk()
master.minsize(300,100)
master.geometry("320x100")

def callback():
print "Clicked!"

photo = PhotoImage(file="add.png")
b = Button(master, image=photo, text="Click Me", command=callback, height=50, width=150, compound=LEFT)
b.place(x=20, y=20)

mainloop()

Button Placement

Want More Insights?
Dive deeper with our downloadable Tkinter examples.

tkinter menu bar

Leveraging the Power of the Tkinter Toolkit: Crafting a Main Menu Bar
For many Python GUI applications, the main menu is not just a feature; it’s the nucleus. Being the focal point, it is the bridge that connects users to various app functionalities.
Here’s an illustrative example of what a menu crafted with Tkinter looks like:
Tkinter menu in Python applications
Recommended Reading:

Constructing the Tkinter Menubar

Designing an intuitive menu using Tkinter is not a herculean task. Here’s a concise and detailed breakdown of the process. Take note that every menu item, like “New”, “Open”, or “Save”, usually maps to a unique callback function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from Tkinter import *

def donothing():
x = 0

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()

The journey of crafting a main menubar kicks off with this fundamental call:

1
menubar = Menu(root)

In this context, root denotes the core Tk() instance.

Menus in graphical interfaces often house a collection of submenus. These can range from File to Edit to View or even Tools. To create these submenus, the pattern remains consistent, utilizing the Menu() call:

1
filemenu = Menu(menubar, tearoff=0)

The final step in the process involves populating these submenus with individual menu items using the add_command() method:

1
filemenu.add_command(label="New", command=donothing)

In our demo here, the donothing() function is a placeholder callback for each menu item. However, in real-world scenarios, every menu item would point to its specific callback function.

Expand Your Horizons: Dive into an ocean of possibilities with in-depth Tkinter tutorials & samples.

tkinter widgets

tkinter messagebox

tkinter filedialog