different widgets in python DECENT INSTITUTE
different widgets in python DECENT INSTITUTE
Tkinter Label
This widget implements a display box where you can place text or images. The text displayed by this
widget can be updated at any time you want.
It is also possible to underline part of the text (like to identify a keyboard shortcut) and span the text
across multiple lines.
Syntax
Here is the simple syntax to create this widget −
w = Label ( master, option, ... )
Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options for this widget. These options can be
used as key-value pairs separated by commas.
Anchor
1 This options controls where the text is positioned if the widget has more space than the text
needs. The default is anchor=CENTER, which centers the text in the available space.
Bg
2
The normal background color displayed behind the label and indicator.
Bitmap
3
Set this option equal to a bitmap or image object and the label will display that graphic.
Bd
4
The size of the border around the indicator. Default is 2 pixels.
5 Cursor
If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that
pattern when it is over the checkbutton.
DECENT COMPUTER INSTITUTE PYTHON MATERIAL
6 Font
If you are displaying text in this label (with the text or textvariable option, the font option
specifies in what font that text will be displayed.
Fg
If you are displaying text or a bitmap in this label, this option specifies the color of the text. If
7
you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the
bitmap.
height
8
The vertical dimension of the new frame.
image
9
To display a static image in the label widget, set this option to an image object.
justify
10 Specifies how multiple lines of text will be aligned with respect to each other: LEFT for flush
left, CENTER for centered (the default), or RIGHT for right-justified.
padx
11
Extra space added to the left and right of the text within the widget. Default is 1.
pady
12
Extra space added above and below the text within the widget. Default is 1.
relief
13 Specifies the appearance of a decorative border around the label. The default is FLAT; for other
values.
text
14 To display one or more lines of text in a label widget, set this option to a string containing the
text. Internal newlines ("\n") will force a line break.
textvariable
15 To slave the text displayed in a label widget to a control variable of class StringVar, set this
option to that variable.
underline
16 You can display an underline (_) below the nth letter of the text, counting from 0, by setting
this option to n. The default is underline=- 1, which means no underlining.
width
17 Width of the label in characters (not pixels!). If this option is not set, the label will be sized to
fit its contents.
wraplength
18 You can limit the number of characters in each line by setting this option to the desired
number. The default value, 0, means that lines will be broken only at newlines.
Tkinter Button
The Button widget is used to add buttons in a Python application. These buttons can display text or
images that convey the purpose of the buttons. You can attach a function or a method to a button which is
called automatically when you click the button.
Syntax
Here is the simple syntax to create this widget −
w = Button ( master, option=value, ... )
Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options for this widget. These options can be
used as key-value pairs separated by commas.
Activebackground
1
activebackground Background color when the button is under the cursor.
Activeforeground
2
Foreground color when the button is under the cursor.
Bd
3
Border width in pixels. Default is 2.
Bg
4
Normal background color.
Command
5
Function or method to be called when the button is clicked.
Font
7
Text font to be used for the button's label.
Height
8
Height of the button in text lines (for textual buttons) or pixels (for images).
Highlightcolor
9
The color of the focus highlight when the widget has focus.
Image
10
Image to be displayed on the button (instead of text).
Justify
11 How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or
RIGHT to right-justify.
Padx
12
Additional padding left and right of the text.
Pady
13
Additional padding above and below the text.
Relief
14 Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE,
and RIDGE.
State
15 Set this option to DISABLED to gray out the button and make it unresponsive. Has the value
ACTIVE when the mouse is over it. Default is NORMAL.
Underline
16 Default is -1, meaning that no character of the text on the button will be underlined. If
nonnegative, the corresponding text character will be underlined.
Width
17
Width of the button in letters (if displaying text) or pixels (if displaying an image).
Wraplength
18
If this value is set to a positive number, the text lines will be wrapped to fit within this length.
Learn Python in-depth with real-world projects through our Python certification course. Enroll and
become a certified expert to boost your career.
4 | DECENT COMPUTER FF-12, AMBER COMPLEX, AJWA RD, BARODA-GUJARAT-INDIA M.+919825754652
DECENT COMPUTER INSTITUTE PYTHON MATERIAL
Methods
Following are commonly used methods for this widget −
flash()
1 Causes the button to flash several times between active and normal colors. Leaves the button
in the state it was in originally. Ignored if the button is disabled.
invoke()
2 Calls the button's callback, and returns what that function returns. Has no effect if the button
is disabled or there is no callback.
Example
Try the following example yourself −
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
def helloCallBack():
msg=messagebox.showinfo( "Hello Python", "Hello World")
B = Button(top, text ="Hello", command = helloCallBack)
B.place(x=50,y=50)
top.mainloop()
When the above code is executed, it produces the following result −
The Entry widget is used to accept single-line text strings from a user.
• If you want to display multiple lines of text that can be edited, then you should use the Text
widget.
• If you want to display one or more lines of text that cannot be modified by the user, then you
should use the Label widget.
Syntax
Here is the simple syntax to create this widget −
w = Entry( master, option, ... )
Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options for this widget. These options can be
used as key-value pairs separated by commas.
Bg
1
The normal background color displayed behind the label and indicator.
Bd
2
The size of the border around the indicator. Default is 2 pixels.
Command
3
A procedure to be called every time the user changes the state of this checkbutton.
Cursor
If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that
pattern when it is over the checkbutton.
Font
4
The font used for the text.
Exportselection
5 By default, if you select text within an Entry widget, it is automatically exported to the
clipboard. To avoid this exportation, use exportselection=0.
Fg
6
The color used to render the text.
Highlightcolor
7
The color of the focus highlight when the checkbutton has the focus.
8 Justify
Relief
9 With the default value, relief=FLAT, the checkbutton does not stand out from its background.
You may set this option to any of the other styles.
selectbackground
10
The background color to use displaying selected text.
selectborderwidth
11
The width of the border to use around selected text. The default is one pixel.
selectforeground
12
The foreground (text) color of selected text.
Show
13 Normally, the characters that the user types appear in the entry. To make a .password. entry
that echoes each character as an asterisk, set show="*".
State
14 The default is state=NORMAL, but you can use state=DISABLED to gray out the control and
make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
Textvariable
15 In order to be able to retrieve the current text from your entry widget, you must set this option
to an instance of the StringVar class.
Width
The default width of a checkbutton is determined by the size of the displayed image or text.
16
You can set this option to a number of characters and the checkbutton will always have room
for that many characters.
xscrollcommand
17 If you expect that users will often enter more text than the onscreen size of the widget, you
can link your entry widget to a scrollbar.
Learn Python in-depth with real-world projects through our Python certification course. Enroll and
become a certified expert to boost your career.
Methods
Following are commonly used methods for this widget −
get()
2
Returns the entry's current text as a string.
icursor ( index )
3
Set the insertion cursor just before the character at the given index.
index ( index )
4 Shift the contents of the entry so that the character at the given index is the leftmost visible
character. Has no effect if the text fits entirely within the entry.
insert ( index, s )
5
Inserts string s before the character at the given index.
select_adjust ( index )
6 This method is used to make sure that the selection includes the character at the specified
index.
select_clear()
7
Clears the selection. If there isn't currently a selection, has no effect.
select_from ( index )
8
Sets the ANCHOR index position to the character selected by index, and selects that character.
select_present()
9
If there is a selection, returns true, else returns false.
select_to ( index )
11 Selects all the text from the ANCHOR position up to but not including the character at the
given index.
xview ( index )
12
This method is useful in linking the Entry widget to a horizontal scrollbar.
Example
Try the following example yourself −
from tkinter import *
top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
top.mainloop()
When the above code is executed, it produces the following result −