Tkinter Components or we can say Tkinter widgets are the helper tool which we can integrate with our GUI for Optimizing purpose and for Proper functioning.
Following are the list of Components provided by Tkinter.
There are some additional advanced components Scale, PanedWindow, LabelFrame, MenuButton, Scrollbar, Progressbar, Tree, Notebook which are not a part of this documentation.
A label is component used to display text just likely the label used in html.
Label(root,
text = 'Display some text',
image = '../path/image.png',
compound = 'none'
).pack()
Parameters/Argument
root is a parent or container name in which the Widget will be placed.
text will display some text attacted to Widget.
image will contain path to the image file or variableName to which the path of the image file is assigned.
compound determines the alignment of image w.r.t. text. The compound option defaults to 'none'.
Parameters | Behaviour |
'none' | Display the image if exist, else display the text. |
'top' | Display the image above the text. |
'bottom' | Display the image below the text. |
'left' | Display the image to the left of the text. |
'right' | Display the image to the right of the text. |
'text' | Display the text, not the image |
'image' | Display the image, not the text. |
A button is same as predominately found on many web GUI and the one like the button tag in html.It supports command parameter just as likely as the html button which supports javascript event(i.e. onclick='', onfocus='').
Button(root,
text = 'Display some text',
command = functionName,
state = '!disabled'
).pack()
Parameters/Argument
root is a parent or container name in which the Widget will be placed.
text will display some text attacted to Widget.
command will contain the name of callback function.
state = '[disabled|!disabled]'(optional) determines is it accessable or not.
An Entry component exactly works same as input tag defined as text in html. It collects the user-input value and stores as string in variable.
inputString = StringVar()
Entry(root,
textvariable = inputString
)
To get the collected value:
variableName.get()is a variable Name is the name of variable to which Entry component is assigned.
While collecting password we can add parameter show = '*' which display '*' while inputing.
A text component is same as the html text-area tag it takes custom user-inputs.
Text(root,
height = intValue
).pack()
Text.insert('1.0', 'Some placeholder text to initially display')
To get current value of Text enterted:
variableName.get('1.0','end') is a variable Name is the name of variable to which Text component is assigned.
A list-box is same as options or select tag in html, it works as a dropdown list to select by.
It allows to select one or multiple items at once.
Listbox(root,
listvariable = tupleName,
height = int_Value,
selectmode = 'modeName'
).pack()
Parameters/Argument
listvariable is a name of variable where the list value is been stored.
height will contain integer value of number of items to show without scrollbar.
The
selectmode option determines whether you can select a single item or multiple items at a time.
- 'browse' :– allows a single selection.
- 'extended' :– allows multiple selection.
By default, the selectmode is 'browse'.
The
curselection() method returns a list of currently selected indices.
Combo-box as the name suggest it is a combination of entry and list-box.
combobox['values'] = ('value1', 'value2', ...)
combobox(root,
textvariable = varName
).pack()
Parameters/Argument
textvariable is a name of variable where the selected value will be stored.
values will contain the list of value to been select from.
state will determines the state of to been act like.
By default, state is normal, in which you can enter a custom value in the combobox.
combobox['state'] = 'normal'
You can set the state option to 'readonly' Mode
combobox['state'] = 'readonly'
A check-box exactly works same as of like a web check-box(html check-box), it can holds a value and calls a callback when it’s been checked or unchecked.
Checkbutton(root,
text = 'Display some text',
command = functionName,
variable = varName,
onvalue = 'checked',
offvalue = 'unchecked'
).pack()
Parameters/Argument
root is a parent or container name in which the Widget will be placed.
text will display some text attacted to Widget.
command will contain the name of callback function.
variable will store the current value of Widget.
onvalue(optional) will return stored value when the Check-Box is checked.
offvalue(optional) will return stored value when the Check-Box is unchecked.
If the checkbox is checked, the value of the variable is 1. Otherwise, it is 0.
A radio-button is the same as that like a web radio-button(html radio-button), it used to select one options from the given.
Each radio-button in the container has a different values, but they have same variable.
Btn1 = Radiobutton(root,
text = 'Display some text',
value = 'value1',
variable = varName,
).pack()
Btn2 = Radiobutton(root,
text = 'Display some text',
value = 'value2',
variable = varName
).pack()
Parameters/Argument
root is a parent or container name in which the Widget will be placed.
text will display some text attacted to Widget.
value will contain the some value.
variable will store the current value of Widget.
A separator component works as a spliter in between other components or widgets. You can set horizontal spliter as well as vertical spliter.
Seperator(root,
orient = '[horizontal|vertical]'
)
Parameters/Argument
root is a parent or container name in which the Widget will be placed.
orient will determines the spliting in horizontal or vertical.
A sizegrip is component which is staticaly located at bottom-right corner of the window which makes or highlights the resizeable feature of the child-window or parent-window.
Child-Window is which is derived or we can say sub-window.
Parent-Window is which is independent and from which the child-window is made and it displays as the program executes till the program get terminated.
parent.resizable(1,1)
child = SizeGrip(parent)
child.pack()
A spin-box is component like entry with some advanced features of button to incease oo decrease input value from the provide range of value.
Spinbox(root,
from_ = intValue,
to = intValue,
textvariable = varName,
wrap = boolValue
).pack()
Parameters/Argument
root is a parent or container name in which the Widget will be placed.
from_ is the minimum value in range.
to is the maximum value in range.
textvariable will store the current value of Widget.
wrap will tell True when current enterted value reaches upwards-max and False when current value reaches downwards-max.
To get the current value:
variableName.get() here variableName is the name of variable to which the Spin-Box is assigned.
A frame is a component which arranges other components in it for the proper allocation and look.
Frame(root,
height = intValue,
width = intValue
)
Parameters/Argument
root is a parent or container name in which the Frame will be placed.
height will set height of the Frame.
width will set width of the Frame.
paddingwill set spacing between the border and components of the Frame.
Frame['padding'] = (left, top, right, bottom)
border will set border of Frame.
Frame['borderwidth'] = intValue
Frame['relief']='styleName'
Border-style of a frame: flat, groove, raised, ridge, solid, sunken. The default border-style of a frame is flat.