Python Tkinter Button



What is the Python Tkinter Button?

The Python Tkinter Button is a standard Tkinter widget. 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.

Tkinter allows us to customize these buttons by allowing buttons to be labels or images.

Button Syntax

object = Button(master, options.....)

Button Options

No.OptionDescription
1 activebackgroundThe Background color when the cursor is over the button.
2 activeforegroundThe Foreground color when the cursor is over the button.
3 bg Background Color of the button
4 bd Border size of frame in pixels. Default is 2.
5 commandCommand to be executed when button is clicked. Typically set to a function call.
6 fgforeground color
7 fontText font for the button
8 heightHeight of the button
9 highlightcolor The text color when the widget is under focus.
10 image Image to be displayed on the button. By default, image will replace the text.
11 justify Changes the alignment of the text. Can be set to either LEFT, CENTER or RIGHT.
12padxpadding to the left and right of the text.
13padypadding above and below the text.
14 relief It specifies the type of the border. Default is Flat, other options include raised, flat, ridge, groove and sunken.
15stateDefault is NORMAL. DISABLED causes the button to gray out and go inactive. ACTIVE is the state of the button when mouse is over it.
16 underlineDefault is -1. Set this option to under line the button text.
17 widthWidth of the button
18 wraplength If the value is set to a positive number, the text lines will be wrapped to fit within this length.

Tkinter Button Example

The following code will create a GUI with a button that displays the text “Hello World” to Console. If you wish to have the output display on the GUI, you should use Python Labels or the Message Box library.

from tkinter import *

def set():
    print("Hello World")

root = Tk()
root.geometry("200x150")

frame = Frame(root)
frame.pack()
button = Button(frame, text = "Button1", command = set)
button.pack()

root.mainloop()

Clicking this button will now activate the function called set and execute the code in it. Be sure to remove the parenthesis while writing the function name, else the function will run once regardless of whether you pressed the button or not.

Tkinter Button

Customizing the button

In this section we’ll show you the same button from the first example, but with a few enhancements to its look.

from tkinter import *

def set():
    print("Hello World")

root = Tk()
root.geometry("200x150")

frame = Frame(root)
frame.pack()
button = Button(frame, text = "Button1", command = set,
                fg = "red", font = "Verdana 14 underline",
                bd = 2, bg = "light blue", relief = "groove")
button.pack(pady = 5)

root.mainloop()
Tkinter Button Customized

This marks the end of the Python Time Delay article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the material in the article can be asked in the comments section below.

Here’s a link back to the Main Tkinter Article where you can learn about other cool widgets!

Leave a Reply

Your email address will not be published. Required fields are marked *