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 GUI applications. Creating a GUI using tkinter is an easy task.
In this article, we will discuss how to print a hard copy in the printer using Tkinter.
- Make a Tkinter window.
- Add One Button.
- Open the file which you want to print using the askopenfilename() method in Tkinter.
- Print it using ShellExecute() method in win32api.
Create Normal Tkinter Window
- askopenfilename: This method is used to open a given file.
filedialog.askopenfilename(mode=’r’, filetypes=[(‘any name you want to display’, ‘extension of file type’)])
- ShellExecute: It is used to execute shell commands of a system.
win32api.ShellExecute(hwnd, dir, bShow, op, file, params, **args)
hwnd: The handle of the parent window, or zero for no parent. This window receives associate message boxes an application produces (for example, for error reporting).
op: The operation to perform. is also “open”, “print”, or None, that defaults to “open”.
file: The name of the file to open.
params: The parameters to pass, if the file name contains associate viable. ought to be None for a document file.
dir: The initial directory for the application.
bShow: Specifies whether the appliance is shown once it’s opened. If the lpszFile parameter specifies a document file, this parameter is zero.
Below is the Implementation
On execution of the above python script, a tkinter window will pop up which will require to upload a text file, after uploading, the text file gets printed.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course