In this tutorial, we will learn how to set the dimensions of the Tkinter window and how to resize it. The dimension means the width and height of the widget in pixels. The default is 200 by 200 pixels which is too small for our purposes, so we will set it to 500 by 500 pixels.
What is Tkinter?
Tkinter is a GUI (graphical user interface) module for the Python programming language. It is used to create graphical user interface (GUI) applications. Tkinter is included with standard Linux, Microsoft Windows and Mac OS X installs of Python.
Tkinter has several built-in widgets that you can use to build your applications. These include:
-Button: A button is a widget that is used to display a text or an image. When the user clicks on the button, an action is performed.
-Canvas: A canvas is a widget that is used to draw or place items on a screen.
-Entry: An entry is a widget that is used to input text.
-Frame: A frame is a widget that is used to group other widgets together. Frames can be used to create layouts within your application.
-Label: A label is a widget that is used to display text or an image. Labels are often used to identify other widgets (such as entry fields).
-Listbox: A listbox is a widget that displays a list of items. The user can select one or more items from the list.
-Menu: A menu is a widget that displays a list of choices. The user can select one of the choices from the menu.
-Message: A message is a widget that displays read-only text. Messages are often used to display information about another widget (such as an entry field).
Tkinter uses so-called event sequences for specifying actions to be bound to certain events. An event sequence is typically bound to a button with the bind method. The general syntax looks like this:
Here, widget is the name of the widget to which the event will be bound, event is a string specifying the kind of event, and handler is the name of the function that will be called when the event occurs. The following table lists some of the most common events:
Button press (left mouse button) Button release (left mouse button) Enter window Leave window Mouse motion</p><br /><h2>Tkinter Geometry Management</h2><br /><p>
There are three geometry managers in Tkinter – pack, grid, and place.
The pack manager is the simplest of the three geometry managers in Tkinter. It just packs widgets in next available space in the window.
The grid manager is the most flexible of the geometry managers in Tkinter. It is used to create a generalised table-like structure.
The place manager is used to position a widget explicitly on the parent widget.
Tkinter Layout Management
There are three geometry managers in Tkinter Pack, Grid and Place.
Pack is the simplest of the three geometry managers of Tk and is the default manager for all widgets. Pack widget is placed one after another in the order of their definition inside the parent widget. By default, each widget has its own top-level window. However, you can also create a parent window that contains other windows (called children windows) within it by using frames. You can also control the position and size of each widget in a Frame by specifying options like padx and pady.
Grid is somewhat similar to pack in that you define a parent widget that will contain other widgets (called children widgets). The major difference between these two geometry managers is that with grid, you specify where each widget should be placed in terms of rows and columns. There are options like rowspan and columnspan, which allow you to span multiple rows or columns with a single widget. You can also use sticky to specify which side of the cell a widget should stick to.
Place is the most flexible of the three geometry managers as it allows you exact control over the position and size of each widget. To do this, you use x and y options to specify how far from the left and top edges of its parent window a widget should be placed respectively. You can also use width and height options to specify how wide and tall a widget should be respectively.
Tkinter Misc Management
Misc management functions live in a separate module called _tkinter. Miscellaneous management functions include:
- quit(): Stop the program by quitting the mainloop.
- mainloop(): Start the program by starting the mainloop.
- destroy(): Destroy a widget, freeing up resources.
- frame = Frame(master, width=200, height=100): A container widget.
- Label(master, text=”Hello world!”).pack(): A very simple label widget.
In conclusion, setting the size of a tkinter window is done by specifying the width and height in pixels. If you want the size to be relative to the screen size, you can use the winfo_screenwidth() and winfo_screenheight() functions. Finally, if you want the window to be a certain percentage of the screen size, you can use the geometry method with a string argument.