python tkinter - ghdrako/doc_snipets GitHub Wiki

Tcl stands for Tool Command Language and it is a scripting language with its own interpreter. On the other hand, Tk is a toolkit for building GUIs. An important point to note is that Tcl/Tk is not Python and we cannot control and access the services of Tcl/Tk using Python. So, another package is introduced and is referred to as tkinter, and it is an intermediator between Python and Tcl/Tk. tkinter will allow us to use the services of Tcl/Tk using the syntax of Python.

Binding of Python to the Tk GUI toolkit will be done by tkinter. tkinter will make everything appear as an object.

Python offers multiple options for developing GUI such as PyQT, Kivy, Jython, WxPython, and pyGUI; tkinter is the most commonly used.

import tkinter as tk
root = tk.Tk()
(...)
root.mainloop()
import tkinter
from tkiner import *
myroot = Tk()
myl1 = Label(myroot, text = 'Label1')
myl1.pack()
myroot.mainloop()

Python tkinter geometry management

All widgets in the tkinter can access geometry management methods. To organize the widgets in the parent windows, the geometry configuration of the widgets is to be accessed, which is offered by tkinter. It will help in managing the display of widgets on the screen. The geometry manager classes are discussed as follows.

pack()

  • fill: This option will determine whether the widgets can increase or grow in size or not. By default, it is NONE. If we want to fill vertically, then it is Y. If horizontally, then it is X. If required both horizontally or vertically, then BOTH.
  • expand: This option will expand the widget to fill any space when set to true or 1. When the window is resized, the widget will expand. • side: This option will decide the widget alignment, that is, against which side of the parent widget packs. By default, it is TOP. The others are BOTTOM, LEFT, or RIGHT.

grid()

This geometry manager will organize the widgets into a 2-Dimensional table, into a number of rows and columns in the parent widget. An intersection of imaginary rows and columns is a cell where each cell in the table can hold a widget.

  • column: This option will use a cell which will be identified with a given column, whose default value is the leftmost column with a numeric value of 0.
  • columnspan: This option will indicate the number of columns the widget occupies; whose default value is 1.
  • padx: This option will add padding to the widget horizontally outside the border of the widget.
  • pady: This option will add padding to the widget vertically outside the border of the widget.
  • ipadx: This option will add padding to the widget horizontally inside the border of the widget.
  • ipady: This option will add padding to the widget vertically inside the border of the widget.
  • row: This option will use a cell that will be identified with a given row whose default value is the first row with numeric value of 0.
  • rowspan: This option will indicate the number of rows the widget occupies whose default value is 1.
  • sticky: Whenever the cell is larger than the widget, an indication is required for the sides and cell corners to which the widget sticks. It may be a string concatenation of 0 or more of compass directions: M, E, S, W, NE, NW, SE, SW, and 0. The widget is centered in the cell with sticky = " and will take up the full cell area when NESW.

place()

This geometry manager will organize the widgets in a specific(fix) position in the parent widget. It is placed at some specific position in the parent widget. It can be used with pack() as well as grid() method.

File selection to process

from tkinter import *
from tkinter.filedialog import askopenfilenames
myroot = Tk()
myroot.title(‘AskOpenFileNames’)
myroot.geometry(‘300x100’)
def myopen_files():
  myfile_list=[]
  myfiles = askopenfilenames(initialdir=”E:\\GUI_Python”,
  title=”Select Python files”)
  for file in myfiles:
    myfile_list.append(file)
  print(myfile_list)
mybtn1 = Button(myroot, text = ‘MyAskOpenFileNames’,command = myopen_files)
mybtn1.pack(pady = 10)
myroot.mainloop()
  • The filedialog module is used to open, save and filter files. Moreover, with this module, we can get information about files and can create custom file dialogs.
  • The askopenfilename() function allows selecting a file to open by the user.
  • The asksaveasfilename() function allows selecting a file to save by the user.
  • The askdirectory() function will return the directory path as a string and displays only directories.
  • The askopenfile() function will return a file handle object and allows only the selection of existing files.
  • The askopenfilenames() function will return file paths as list of strings and allows multiple selections.
  • The asksaveasfile will return a file handle object and allows new file creations and confirmation on existing files to be prompted.