- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - 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.
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 & Description|
Background color when the button is under the cursor.
Foreground color when the button is under the cursor.
Border width in pixels. Default is 2.
Normal background color.
Function or method to be called when the button is clicked.
Normal foreground (text) color.
Text font to be used for the button's label.
Height of the button in text lines (for textual buttons) or pixels (for images).
The color of the focus highlight when the widget has focus.
Image to be displayed on the button (instead of text).
How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.
Additional padding left and right of the text.
Additional padding above and below the text.
Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.
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.
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 of the button in letters (if displaying text) or pixels (if displaying an image).
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.||Medthod & Description|
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.
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 −
# !/usr/bin/python3 from tkinter import * from tkinter import messagebox top = Tk() top.geometry("100x100") def helloCallBack(): msg = messagebox.showinfo( "Hello Python", "Hello World") B = Button(top, text = "Hello", command = helloCallBack) B.place(x = 50,y = 50) top.mainloop()
When the above code is executed, it produces the following result −