Google News
logo
Tkinter Interview Questions
The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including macOS, as well as on Windows systems.

Tkinter supports a range of Tcl/Tk versions, built either with or without thread support. The official Python binary release bundles Tcl/Tk 8.6 threaded. See the source code for the _tkinter module for more information about supported versions.
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task.
 
To create a tkinter app :
 
1. Importing the module – tkinter
2. Create the main window (container)
3. Add any number of widgets to the main window
4. Apply the event Trigger on the widgets.
 
 
Importing tkinter is same as importing any other module in the Python code. Note that the name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’.
App and Root are two different objects. root is the root window into which all other widgets go. It is an instance of the class Tk, and every tkinter application must have exactly one instance of this class. app is an instance of the class App, which is a subclass of Frame. A frame is typically used as a container for other widgets, in this case the widgets that make up the app (except for the root window). This frame is packed inside the root window. app and root are two completely different things. root is a container for app.
The pack() widget is used to organize widget in the block. The positions widgets added to the python application using the pack() method can be controlled by using the various options specified in the method call.
 
However, the controls are less and widgets are generally added in the less organized manner.
 
Syntax : widget.pack(options)
The grid() geometry manager organizes the widgets in the tabular form. We can specify the rows and columns as the options in the method call. We can also specify the column span (width) or rowspan(height) of a widget.
 
This is a more organized way to place the widgets to the python application. The syntax to use the grid() is given below.
 
Syntax : widget.grid(options)
The place() geometry manager organizes the widgets to the specific x and y coordinates.
 
Syntax : widget.place(options) 
expand : When set to true, widget expands to fill any space not otherwise used in widget's parent.
 
fill : Determines whether widget fills any extra space allocated to it by the packer, or keeps its own minimal dimensions: NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically).
 
side : Determines which side of the parent widget packs against: TOP (default), BOTTOM, LEFT, or RIGHT.
Column : The column number in which the widget is to be placed. The leftmost column is represented by 0.
 
Columnspan : The width of the widget. It represents the number of columns up to which, the column is expanded.
 
ipadx, ipady : It represents the number of pixels to pad the widget inside the widget's border.
 
padx, pady : It represents the number of pixels to pad the widget outside the widget's border.
 
row : The row number in which the widget is to be placed. The topmost row is represented by 0.
 
rowspan : The height of the widget, i.e. the number of the row up to which the widget is expanded.
 
Sticky : If the cell is larger than a widget, then sticky is used to specify the position of the widget inside the cell. It may be the concatenation of the sticky letters representing the position of the widget. It may be N, E, W, S, NE, NW, NS, EW, ES.
 
 
Anchor : It represents the exact position of the widget within the container. The default value (direction) is NW (the upper left corner)
 
bordermode : The default value of the border type is INSIDE that refers to ignore the parent's inside the border. The other option is OUTSIDE.
 
height, width : It refers to the height and width in pixels.
 
relheight, relwidth : It is represented as the float between 0.0 and 1.0 indicating the fraction of the parent's height and width.
 
relx, rely : It is represented as the float between 0.0 and 1.0 that is the offset in the horizontal and vertical direction.
 
x, y : It refers to the horizontal and vertical offset in the pixels.
The Text widget accepts multiline user input, where you can type text and perform operations like copying, pasting, and deleting. There are certain ways to disable the shortcuts for various operations on a Text widget.
 
In order to disable copy, paste and backspace in a Text widget, you’ve to bind the event with an event handler and return break using lambda keyword in python.