python logo

tkinter button


Python hosting: Host, run, and code Python in the cloud!

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.

← Previous Guide | Next Guide →





Leave a Reply:




Fabi.Choi Mon, 18 May 2015

PhotoImage function doesn't open 'png' files.
It can be open 'GIF, PGM and PPM'.

follow this article : http://stackoverflow.com/qu...

Frank Mon, 18 May 2015

Thanks for the comment! This interesting. PhotoImage function opens PNG under Linux. Traditional formats are GIF, PGM and PPM, perhaps the Linux version of the Tk package is custom or you have an old Tkinter version. Do you have the latest Tkinter version?

The developers of Tk mention: "Built-in PNG Image Support: Photo images now support read/write in the PNG format, with the ability to set the alpha channel." http://www.tcl.tk/software/tcltk/8.6.html

I'll have a look when I'm on a windows machine.

Paasl Tue, 25 Aug 2015

import ImageTk
...
photo=ImageTk.PhotoImage(file="add.png")

Paul Tue, 06 Oct 2015

when i import the wx and Tkinter, neither is working. I get an import error message and it says that there is no module named wx or Tkinter. Do i have to download these modules or what should i do? Am using python 3.4.3 on win 8.1. Thanks.

Frank Wed, 07 Oct 2015

Hi, you should install either of these two modules. You could use pip or easyinstall for that.
If you use a development editor such as pycharm you can do that from the editor, otherwise:
See: http://www.wxpython.org/download.php