How to design Button in Tkinter GUI application : Button Widget


 

Button Widget

Button is widely used widget to trigger an event as per requirement like
  • browse
  • save
  • open
  • close etc. 
Button can created as
  • text can be displayed
  • image can also be displayed
Syntax:

            Button(master, options.........)

master : master is nothing but the window object (parent window for which button needs to be created )

options : options are used to shape the button separated by comma's and to define action on click. Below is the list of options, can be passed while creating button.


  • text : 
    • to display text on button
  • activebackground : 
    • decides background colour of button when clicked on button.
  • activeforeground : 
    • decides foreground colour of text on button when click on button on button.
  • bd : 
    • to define button border
  • bd : 
    • to give button a background colour
  • fg  : 
    • to give text a colour
  • command : 
    • action to preform when button is clicked
  • font : 
    • to decide text font
  • height : 
    • to give button a height
  • highlightcolor : 
    • to provide colour of focus
  • image : 
    • image to be displayed on button
  • justify : 
    • to control how to display text
      • LEFT to justify left
      • CENTER to justify center
      • RIGHT to justify right
  • padx : 
    • additional padding of button text from left and right
  • pady
    • additional padding of button text from top and bottom
    • relief : 
      • it specifies the type of border
        • SUNKEN
        • RAISED
        • GROOVE
        • RIDGE
    • state : 
      • to make button unresponsive
        • ACTIVE
        • NORMAL
    • underline : 
      • to underline the text on button
        • default is -1, states no underline
        • any positive vale will underline the text
    • width : 
      • to define button's width
    • wraplength : 
      • to wrap the text lines within the passed length


    there are few options which may not work on mac, but work finely on windows and linux


    Example for the Button widget, output screen shot is on top of this post


    # program to create Button widget

    try:
    from Tkinter import *
    except:
    from tkinter import *

    mw = Tk()
    mw.geometry(
    '500x100+10+10')
    mw.title(
    "AETies : Automation Expert Technologies")

    Button = Button(mw
    , text="click here, I am a Button")
    Button.pack()

    mw.mainloop()



    Comments

    My photo
    Techno Xpresss
    Bangalore, Karnataka, India