How to Print Hard Copy using Tkinter?

24 Jan, 2021
Prerequisite: Tkinter, win32api
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.
Python3
# Import Required Libraryfrom tkinter import *  # Create Tkinter Objectroot = Tk()   # Set Title and geometryroot.title('Print Hard Copies') root.geometry("200x200")  # Make ButtonButton(root,text="Print FIle").pack()  # Execute Tkinterroot.mainloop()
Output:
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
Python3
# Import Required Libraryfrom tkinter import *import win32apifrom tkinter import filedialog  # Create Tkinter Objectroot = Tk()  # Set Title and geometryroot.title('Print Hard Copies')root.geometry("200x200")  # Print File Functiondef print_file():        # Ask for file (Which you want to print)    file_to_print = filedialog.askopenfilename(      initialdir="/", title="Select file",       filetypes=(("Text files", "*.txt"), ("all files", "*.*")))          if file_to_print:                # Print Hard Copy of File        win32api.ShellExecute(0, "print", file_to_print, None, ".", 0)  # Make ButtonButton(root, text="Print FIle", command=print_file).pack()  # Execute Tkinterroot.mainloop()
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.
Python Tkinter-exercises
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function