We now have a youtube channel. Subscribe!

Python Tkinter Button Widget



Hello dear readers! welcome back to another section of our tutorial on Python. In this tutorial post, we are going to be discussing about Python Tkinter Button Widget.

The Button widget is used for adding buttons in Python. These buttons can show text or images that convey the purpose of the buttons. You can attach a function or a method to the button which is automatically called when you click on the button.

Syntax

The following below is the syntax for create this widget -

w = Button ( master, option=value, ... )

Parameter Details

  • master - This represents the parent window.
  • options - Following below is the list of commonly used options for this widget. These options can be used as key-value pairs that are separated by commas.


Sr.No.Option & 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
Border width in pixels. Default is 2.
4
bg
Normal background color.
5
command
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
Additional padding left and right of the text.
13
pady
Additional padding above and below the text.
14
relief
Relief specifies the type of the border. Some of the values 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. Default is NORMAL.
16
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.
17
width
Width of the button in letters (if displaying text) or 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.


Methods

Following are the list of commonly used methods for this widget -

Sr.No.Method & Description
1
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.
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.

Example

Following is a simple example -

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()

Output
When the above code is executed, it will produce the following result -



Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python Tkinter Canvas Widget.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain