Python - Tkinter Button

The Button widget is used to add buttons in a Python application. These buttons can 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 automatically when you click the button.
Here is the simple syntax to create this widget −
w = Button ( master, option=value, ... )

master − This represents the parent window.
master − 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.
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.
activebackground
Background color when the button is under the cursor.
activeforeground
Foreground color when the button is under the cursor.
bd
Border width in pixels. Default is 2.
bg
Normal background color.
command
Function or method to be called when the button is clicked.
fg
Normal foreground (text) color.
font
Text font to be used for the button's label.
height
Height of the button in text lines (for textual buttons) or pixels (for images).
highlightcolor
The color of the focus highlight when the widget has focus.
image
Image to be displayed on the button (instead of text).
justify
How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.
padx
Additional padding left and right of the text.
pady
Additional padding above and below the text.
relief
Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.
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. Default is NORMAL.
underline
Default is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined.
width
Width of the button in letters (if displaying text) or pixels (if displaying an image).
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 −
flash()
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.
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.
Try the following example yourself −
import Tkinter
import tkMessageBox

top = Tkinter.Tk()

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

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

B.pack()
top.mainloop()
When the above code is  executed, it produces the following result −

    187 Lectures 
     17.5 hours 

 Malhar Lathkar

    55 Lectures 
     8 hours 

 Arnab Chakraborty

    136 Lectures 
     11 hours 

 In28Minutes Official

    75 Lectures 
     13 hours 

 Eduonix Learning Solutions

    70 Lectures 
     8.5 hours 

 Lets Kode It

    63 Lectures 
     6 hours 

 Abhilash Nelson
 Print
 Add Notes
 Bookmark this page