Enter your email id to get regular update free
Login
Your Email
Pasword

Using Button and Label in python GUI

As we know button, label and text are one of the basic components to develop a website or application. In this article we will learn how to use these components (i.e. button, label and text) in python. Here we will be using the python tkinter package or module to implement our components.

As we know button, label and text are some of the basic components to develop a website or application. In this article, we will learn how to use these components (i.e. button, label, and text) in python. Here we will be using the python tkinter package or module to implement our components.
1. Button: 
The Button component is used to display buttons within a python application. 
Syntax: 
                   sp = Button ( master, option=value1,value2 ... ) 
 Where, 
master represents the parent window. 
options is the list of most commonly used options for buttons. 
Some of the button options are given below, 
I. activebackground : Sets background color when the cursor is above the button. 
II. activeforeground: Sets foreground color when the cursor is above the button. 
III. bd: Sets border width in pixels. By default, it is 2 pixels. 
IV. bg: Sets a normal background color. 
V. command: Sets function or method to be invoked when the button is clicked. 
VI. fg: Sets normal foreground text color. 
VII. font: This is the text font that is to be used for the button's label. 
VIII. height: Defines the height of the button in text lines for textual buttons or pixels for images. 
IX. width: Defines the width of the button in letters if displaying text or pixels if displaying an image. 
X. highlightcolor: Sets the color/ highlight when the component has focus. 
XI. image: Used to set the image to be displayed on the button instead of text. 
XII. justify: Defines how multiple text lines will be shown. There are three types of justification, they are, LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify. 
XIII. underline: Default value for underline is -1, meaning that no character of the text on the button will be underlined. If the value is nonnegative, the corresponding text character will be underlined. 
XIV. padx: Adds padding to the left and right of a text. 
XV. pady: Adds padding to above and below a text. 
XVI. state: Used to set this option to DISABLED to make the button unresponsive. It has the value ACTIVE when the mouse is over it. By default the value is NORMAL. 
XVII. wraplength: If this value of this component is set to a positive number, then the text lines will be wrapped to fit within the length. 
 Some common methods in button: 
• flash(): This method causes the button to flash several times between active and normal colors. The flash() method is ignored if the button is disabled. 
• invoke(): This method calls the button's callback and returns whatever that function returns. The invoke() method has no effect if the button is disabled 
Example: 
import Tkinter 
import tkMessageBox  
top = Tkinter.Tk() 
def callBack(): 
        tkMessageBox.showinfo( "Hello Python", "Hello World") 
sp = Tkinter.Button(top, text ="Hello", command = callBack) 
sp.pack() 
top.mainloop() 
 Output: 
2. Label: This component of the python Tkinter package implements a display box where the user can place text or images. It is also possible to underline the part of the text (like which is used to identify a keyboard shortcut). 
Syntax 
                   sp = Label ( master, option, ... ) 
 Where 
• master represents the parent window. 
• options is the list of most commonly used options for a label.  
Some of the button options are given below, 
I. anchor : Anchor is used to controls where the text is positioned if the widget has more space than the text needs. By default, anchor=CENTER, which center align the text. 
II. bg: The bg option is used for the normal background color displayed behind the label. 
III. bd: The bd option is used to define the size of the border around the label. The default value is 2 pixels. 
IV. bitmap: Set the bitmap option equal to a bitmap or image object and the label will display either a bitmap or image graphic. 
V. relief: The relief option is used to specifies the appearance of a decorative border around the label. The default value is FLAT. 
VI. image: The image option is used to display a static image in the label component. 
VII. font: The font option specifies in what font that text will be displayed within a label. 
VIII. width: Width of the label in characters. If the width option is not set, the label will be sized to fit its contents. 
IX. height: Height is used to define the vertical dimension of the height. 
X. justify: Defines how multiple text lines will be shown. There are three types of justification, they are, LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify. 
XI. underline: The underline option is used to display an underline below the nth letter of the text, by setting this option to n. The default value is underline=-1, which means there will be no underlining. 
XII. padx: Padx is used to add extra space to the left and right of the text within the label. By default value is 1. 
XIII. pady: Pady is used to add extra space added above and below the text within the label. By default value is 1. 
XIV. wraplength: Wraplength can be used to limit the number of characters in each line by setting this option to the desired number. The default value is zero which means that lines will be broken only at newlines. 
Example: 
from Tkinter import * 
root = Tk() 
var = StringVar() 
label = Label( root, textvariable=var, relief=RAISED ) 
var.set("Hey!? How are you doing?") label.pack() root.mainloop()  
Output: 


Python button and label
learn python GUI
Using button and label in python
python button tutorial
python label tutorial
Educational
Samiran Panda
Lecturer
Total 105 views