Home » Python Tkinter Button

Python Tkinter Button

by maxguy71

In this article we look at the tKinter button

These buttons are able to display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called when you click on the button.

The syntax to use the button widget is given below.


W = Button(parent, options)


  • parent− This represents the parent window.
  • options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Sr.No. Option with Description
1 activebackground

Background color when the button is under the cursor.

2 activeforeground

Foreground color when the button is under the cursor.

3 bd

The border width in pixels. Default is 2.

4 bg

Normal background color.

5 command

The function or method to be called when the button is clicked.

6 fg

Normal foreground (text) color.

7 font

Text font to be used for the button’s label.

8 height

Height of the button in text lines (for textual buttons) or pixels (for images).

9 highlightcolor

The color of the focus highlight when the widget has focus.

10 image

Image to be displayed on the button (instead of text).

11 justify

How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.

12 padx

This specifies any additional padding left and right of the text.

13 pady

This specifies any additional padding above and below the text.

14 relief

Relief specifies the type of the border of the button. Some of the values that are supported are SUNKEN, RAISED, GROOVE, and RIDGE.

15 state

Set this option to DISABLED to gray out the button and make it unresponsive. Has the value ACTIVE when the mouse is over it. The default is NORMAL.

16 underline

The default is -1, this means that no character of the text on the button will be underlined. If non-negative, the corresponding text character will be underlined.

17 width

Width of the button in letters if displaying text or in pixels if displaying an image

18 wraplength

If this value is set to a positive number, the text lines will be wrapped to fit within this length.


Following are commonly used methods for this widget −

Sr.No. Method & Description
1 flash()

This causes the button to flash several times between active and normal colors. Leaves the button in the state it was in originally. Ignored if the button is disabled.

2 invoke()

Calls the button’s callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback.



Now for some examples

#python application to create a simple button   
from tkinter import *   
top = Tk()   
b = Button(top,text = "Simple")  

and now attach a function when the button is pressed which will display a messagebox

import Tkinter
import tkMessageBox

top = Tkinter.Tk()

def helloCallBack():
   tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello", command = helloCallBack)



from tkinter import *

class Application(Frame):
    def hello(self):
        print ("hello world, I'm tkinter")

    def createGUI(self):
        #a button with red text on a black background 
        #that says EXIT that quits the app
        self.QUIT = Button(self)
        self.QUIT["text"] = "EXIT"
        self.QUIT["fg"]   = "yellow"
        self.QUIT["bg"]   = "black"
        self.QUIT["command"] =  self.quit
        self.QUIT.pack({"side": "left"})
        #a button that will say the contents of hello on
        #the comamnd line
        self.hi_there = Button(self)
        self.hi_there["text"] = "Hello",
        self.hi_there["command"] = self.hello
        self.hi_there.pack({"side": "left"})

    def __init__(self, master=None):
        Frame.__init__(self, master)

root = Tk()
app = Application(master=root)

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More