# Example application -- tkwidget_print.py # # Jochim Thöne July 2003 JoachimThoene@web.de #--------------------------------------------------------------------------------------------------------- # Paper printing of a Tkinter widget in WIN32 enviroment # # Requirements: # Python, Tkinter, Python Win32 Extensions and Python Imaging Library (version => 1.1.4) # #--------------------------------------------------------------------------------------------------------- # # This example application uses: # # Python # Copyright © 2001, 2002 Python Software Foundation. # All Rights Reserved. # # The Python Imaging Library # Copyright © 1997-2002 by Secret Labs AB # Copyright © 1995-2002 by Fredrik Lundh # # Python for Win32 Extensions # Copyright © 1994-2001 Mark Hammond (mhammond@skippinet.com.au) # #--------------------------------------------------------------------------------------------------------- # Note # This application is only a first try of paper printing a tkinter widget. The PIL library contains many # possibilities to improve the paperprints output quality by handling the grabbed input image with PIL. # Also the paperdesign could be improved using Win32 Extensions(for Example: where to place the image on # the sheet). ---------------------------------------------------------------------------------------------------------- from Tkinter import * import Image, ImageWin, ImageGrab import os import win32ui import win32print import win32con # funtion to grab the widget and return a bitmap object def grabwidget(widget): x = widget.winfo_rootx() y = widget.winfo_rooty() w = widget.winfo_width() h = widget.winfo_height() print x, y, w, h return ImageGrab.grab((x+2, y+2, x+w-2, y+h-2)) # +2 and -2 to avoid printing # the widgets border # grabs widget and stores it as a file def print_file(): text.update() image = grabwidget(text) image.save("widget.bmp") # grabs widget, converts it to a Dib object and sends it to the # systems standard printer def print_text(): text.update() text.update_idletasks() image = grabwidget(text) image.save("tmp.bmp") # Creates a Dib instance from grabbed file im = Image.open("tmp.bmp") dib = ImageWin.Dib('RGB',im.size) dib.paste(im,None) imagesize = (im.size[0]*10, im.size[1]*10) # Sends the dib to the windows standard printer printer = win32print.GetDefaultPrinter() print printer phandle = win32print.OpenPrinter(printer) dc = win32ui.CreateDC() dc.CreatePrinterDC() dc.StartDoc(im.filename) dc.StartPage() dib.draw(dc.GetHandleAttrib(), (0, 0) + imagesize) # output size can be changed dc.EndPage() # by modifying the im.size tuple dc.EndDoc() win32print.ClosePrinter(phandle) del dc del im os.unlink("tmp.bmp") # This terminates the application def close_all(): root.quit() # Simple text widget with 3 buttons root = Tk() frame = Frame(root) frame.pack() text = Text(frame,width = 50, height = 30, bg = "white") text.pack() button = Button(frame,text = "Bitmap to File", command = print_file) button.pack(side = LEFT, expand = "yes", fill = X) button1 = Button(frame,text = "Bitmap to Printer", command = print_text) button1.pack(side = LEFT, expand = "yes", fill = X) button2 = Button(frame,text = "Exit", command = close_all) button2.pack(side = LEFT, expand = "yes", fill = X) root.mainloop()