Python Tkinter Button

python tutorials and learn python

Created with Sketch.

Python Tkinter Button

The button widget is used to add various types of buttons to the python application. Python allows us to configure the look of the button according to our requirements. Various options can be set or reset depending upon the requirements.

We can also associate a method or function with a button which is called when the button is pressed.

The syntax to use the button widget is given below.

Syntax

  1. W = Button(parent, options)

A list of possible options is given below.

SNOptionDescription
1activebackgroundIt represents the background of the button when the mouse hover the button.
2activeforegroundIt represents the font color of the button when the mouse hover the button.
3BdIt represents the border width in pixels.
4BgIt represents the background color of the button.
5CommandIt is set to the function call which is scheduled when the function is called.
6FgForeground color of the button.
7FontThe font of the button text.
8HeightThe height of the button. The height is represented in the number of text lines for the textual lines or the number of pixels for the images.
10HighlightcolorThe color of the highlight when the button has the focus.
11ImageIt is set to the image displayed on the button.
12justifyIt illustrates the way by which the multiple text lines are represented. It is set to LEFT for left justification, RIGHT for the right justification, and CENTER for the center.
13PadxAdditional padding to the button in the horizontal direction.
14padyAdditional padding to the button in the vertical direction.
15ReliefIt represents the type of the border. It can be SUNKEN, RAISED, GROOVE, and RIDGE.
17StateThis option is set to DISABLED to make the button unresponsive. The ACTIVE represents the active state of the button.
18UnderlineSet this option to make the button text underlined.
19WidthThe width of the button. It exists as a number of letters for textual buttons or pixels for image buttons.
20WraplengthIf the value is set to a positive number, the text lines will be wrapped to fit within this length.

Example

  1. #python application to create a simple button
  2. from tkinter import *
  3. top = Tk()
  4. top.geometry(“200×100”)
  5. b = Button(top,text = “Simple”)
  6. b.pack()
  7. top.mainaloop()

Output:

Python Tkinter Button

Example

  1. from tkinter import *
  2. top = Tk()
  3. top.geometry(“200×100”)
  4. def fun():
  5.     messagebox.showinfo(“Hello”“Red Button clicked”)
  6. b1 = Button(top,text = “Red”,command = fun,activeforeground = “red”,activebackground = “pink”,pady=10)
  7. b2 = Button(top, text = “Blue”,activeforeground = “blue”,activebackground = “pink”,pady=10)
  8. b3 = Button(top, text = “Green”,activeforeground = “green”,activebackground = “pink”,pady = 10)
  9. b4 = Button(top, text = “Yellow”,activeforeground = “yellow”,activebackground = “pink”,pady = 10)
  10. b1.pack(side = LEFT)
  11. b2.pack(side = RIGHT)
  12. b3.pack(side = TOP)
  13. b4.pack(side = BOTTOM)
  14. top.mainloop()

Output:

Python Tkinter Button
Python Tkinter Button

 

Leave a Reply

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