Open In App

How to Print Hard Copy using Tkinter?

Last Updated : 24 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Step-by-step Approach

  • 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

Python3




# Import Required Library
from tkinter import *
  
# Create Tkinter Object
root = Tk() 
  
# Set Title and geometry
root.title('Print Hard Copies'
root.geometry("200x200")
  
# Make Button
Button(root,text="Print FIle").pack()
  
# Execute Tkinter
root.mainloop()


Output:

Methods Used

  • 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 Library
from tkinter import *
import win32api
from tkinter import filedialog
  
# Create Tkinter Object
root = Tk()
  
# Set Title and geometry
root.title('Print Hard Copies')
root.geometry("200x200")
  
# Print File Function
def 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 Button
Button(root, text="Print FIle", command=print_file).pack()
  
# Execute Tkinter
root.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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads