Printing From Tkinter Or Pmw Intrerface
Solution 1:
It depends on whether you are using windows or linux or mac/unix. The answer will change accordingly depending on which system you are running this python script on.
WINDOWS
For sending print jobs to a printer on windows, you will use win32print
http://timgolden.me.uk/python/win32_how_do_i/print.html
LINUX/UNIX/MAC
For linux/unix/mac systems, you can use the pkipplib
library and leverage on the CUPS (http://en.wikipedia.org/wiki/CUPS) A computer running CUPS is a host that can accept print jobs from client computers, process them, and send them to the appropriate printer.
See http://www.pykota.com/software/pkipplib for CUPS-based printing.
pkipplib
is a 3rd party library that can installed via PyPi. If you are familiar with the use of pip
, all you need to do is to run
pip install pkipplib
and the pkipplib
python module will be available for you to import into your script. You can then execute printing after making a connection to your linux/unix/mac CUPS printer similar to the example in the above link. Like this:-
# CUPS' APIfrom pkipplib import pkipplib
# Create a CUPS client instance # cups = pkipplib.CUPS(url="http://server:631, \# username="john", \# password="5.%!oyu")
cups = pkipplib.CUPS()
# High level API : retrieve info about job 3 :
answer = cups.getJobAttributes(3)
print answer.job["document-format"]
# That's all folks !# Lower level API :
request = cups.newRequest(pkipplib.IPP_GET_PRINTER_ATTRIBUTES)
request.operation["printer-uri"] = ("uri",
cups.identifierToURI("printers", "HP2100"))
for attribute in ("printer-uri-supported",
"printer-type",
"member-uris") :
# IMPORTANT : here, despite the unusual syntax, we append to # the list of requested attributes :
request.operation["requested-attributes"] = ("nameWithoutLanguage", attribute)
# Sends this request to the CUPS server
answer = cups.doRequest(request)
# Print the answer as a string of textprint answer
Solution 2:
The simplest way to do this is to create a text file and then use the OS print command (eg lpr on *nix). You can incorporate markup (groff or latex) if necessary) You could also create a postscript or PDF file but that's much more work!
This has the added advantage that the print command can be made configurable(environment or config file) and thus work on multiple OS.
Solution 3:
I hope this will help. Yes, you can use win32com.client
To dispatch the file for printing you can use openpyxl to
Your data into xl spreadsheet and send them for print with default or desired printer for more information read win32com.client to send data for print
Solution 4:
Good question. Hope my answer will help.
Note: I use python 3.3 so thats why we need to import tkinter and Tkinter...
In the procedural code lines below, you need to define a text variable as string-variable (here it's text1
) with = StringVar()
.
Then when you create the entry widget (here its text_entry
) you indicate which variable takes the value entered when the user hit the button (.., textvariable = text1)
.
Then you create a button widget with command =
the function (here it's print_text(*args))
which will print the things the user typed.
Since text1 is a list (or maybe tuple, not sure) you need to use the attribute's .get
method to get the value entered. Here its print(text1.get())
I added I few more things: text_entry.focus()
puts the cursor in that widget when you run the program so that the user doesnt have to click in the entry space and root.bind('<Return>', print_text)
allows the user to hit Enter to print.
from tkinter import *
from tkinter import ttk
def print_text(*args):
try:
print(text1.get())
except ValueError:
pass
root = Tk()
root.title("Little tkinter app for printing")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
text1 = StringVar()
text_entry = ttk.Entry(mainframe, width = 20, textvariable=text1)
text_entry.grid(column = 1, row = 2, sticky = (N,W,E,S))
ttk.Button(mainframe, text = "Print!", command =
print_text(text1)).grid(column = 1, row = 3, sticky = (E))
for child in mainframe.winfo_children():
child.grid_configure(padx = 5, pady = 5)
text_entry.focus()
root.bind('<Return>', print_text)
root.mainloop()
Post a Comment for "Printing From Tkinter Or Pmw Intrerface"