Tkinter Button

Python Tkinter button is one of the most popularly used Tkinter Widgets in python for designing buttons in GUI’s. A button is used as a way for the user to interact with the User interface. Once the button is clicked, an action is triggered by the program. These buttons can display text or images that convey the purpose of the buttons.

Syntax:

The parameter, master represents the parent window and the options are used to set various attribute as key-values separated by commas. The options are described below

Options of Tkinker Button:

fg: The color of the text label placed on the button.

bg: The initial background color of the button.

bd: border-width of the button.

font: Font of the text to be used in the label of the button.

active foreground: Color of the text in the button when it gets clicked.

active background: Color of the background area of the button when it gets clicked.

image: If an image needs to be used instead of a text in the button.

justify: How the text to be aligned in the button, whether it needs to be left-justified or right-justified else in the center.

height: Represents the height of the button which is used.

highlight color: Focus highlight color

Command: Function or method to be called when the button is clicked.

State: The option needs to be set when the mouse is over it.

pady: The padding area between the label and the button corners is above and below the area of the button.

DISABLES: Gray out the button

Relief: Mentions the variety of the border that needs to be used, some among the usual values are SUNKEN, GROOVE, RAISED, and RIDGE.

Underline: Used to underline the text of the label in the button, when the value is -1 nothing remains underlined whereas when the value is nonnegative then the corresponding character in the text will be underlined.

active: The button remains active.

padx: The padding area between the label and the button corners on the left and right side of the button.

Width: Mentions the width of the button.

Wrap Length: Allow wrapping the text label.

Methods of Tkinker Button:

There are two methods of Tkinker Button as described below;

  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:
from tkinter import *

appWindow = Tk()
appWindow.title("Window Title-Tutorialsart.com")

appWindow.geometry('350x200')

labell = Label(appWindow, text="tkinter Label")
labell.grid(column=0, row=0)
btn = Button(appWindow, text="tkinter Button", bg="orange")
btn.grid(column=1, row=0)

appWindow.mainloop()
Output:
12 tkinter button

Comments are closed.