Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Tkinter Button Widget

Python Tkinter Button Widget

Python GUI Programming Python GUI Programming

The Tkinter Button widget is used to add buttons to a Python application. A button can display text or an image, can be used to listen for user actions, and can be associated with a Python function that is called automatically when the button is pressed.

Syntax

The syntax format is as follows:

w = Button ( master, option=value, ... )
No. Options & Description
1

activebackground

The background color of the button when the mouse hovers over it

2

activeforeground

The foreground color of the button when the mouse hovers over it

3

bd

The size of the button border, defaulting to 2 pixels

4

bg

The background color of the button

5

command

The function associated with the button, executed when the button is clicked

6

fg

The foreground color of the button (the color of the button text)

7

font

The text font

8

height

The height of the button

9

highlightcolor

The color to use for highlighting

10

image

The image to display on the button

11

justify

When displaying multi-line text, set the alignment between different lines; options include LEFT, RIGHT, CENTER

12

padx

The padding of the button in the x direction, i.e., the distance between the button's content and its edge

13

pady

The padding of the button in the y direction

14

relief

Border style, setting the 3D effect of the widget. Options are: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. The default is FLAT.

15

state

Set the state of the button widget. Options are NORMAL, ACTIVE, DISABLED. The default is NORMAL.

16

underline

Underline. By default, text on the button has no underline. The value is the string index to underline: when 0, the first character is underlined; when 1, the first two characters are underlined, and so on

17

width

The width of the button; if not set, its size adapts to the button's content (the size of the text or image)

18

wraplength

Limit the number of characters displayed per line on the button

19

text

The text content of the button

19

anchor

The anchor option, which controls the position of the text; the default is center

Method

The following are commonly used methods of the widget:

Method Description
deselect() Clear the state of the radio button
flash() Blink the radio button several times between the active-state color and the normal color, but keep its original state.
invoke() You can call this method to achieve the same action that occurs when the user clicks the radio button to change its state
select() Set the radio button to selected.

Example

In the example, clicking the button displays a message:

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

Tkinter

import

tkMessageBox

top

=

Tkinter

.

Tk

(

)

def

helloCallBack

(

)

:

tkMessageBox

.

showinfo

(

"

Hello Python

"

,

"

Hello Runoob

"

)

B

=

Tkinter

.

Button

(

top

,

text

=

"

Click me

"

,

command

=

helloCallBack

)

B

.

pack

(

)

top

.

mainloop

(

)

The test output is as follows:

Python GUI Programming Python GUI Programming