Skip to content

Instantly share code, notes, and snippets.

@mutaku
Created February 28, 2012 02:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutaku/1928574 to your computer and use it in GitHub Desktop.
Save mutaku/1928574 to your computer and use it in GitHub Desktop.
Printer selection in a Tkinter Python GUI using lpstat and test printing with lpr
#!/usr/bin/env python
from Tkinter import *
import sys, commands, os
root = Tk()
root.title('Printer Configuration')
root.config(bg="lightblue")
def getPrinter():
nIndex = printerList.curselection()[0]
sActivePrinter = printerList.get(nIndex)
setPrinter(sActivePrinter)
def setPrinter(printerVar):
os.remove('printerSelection.py')
file = open('printerSelection.py', 'w')
print >>file, 'PRINTER = '+printerVar
quit()
def testPrinter():
nIndex = printerList.curselection()[0]
sActivePrinter = printerList.get(nIndex)
printerControl = os.popen('lpr -P %s' % (sActivePrinter), 'w')
printerControl.write('Laborder Configration Tool Test Print')
printerControl.close()
frame1 = Frame(root)
frame1.config(bg="lightblue", padx=2, pady=2)
frame1.pack()
frame2 = Frame(root)
frame2.config(bg="lightblue")
frame2.pack()
header = 'Laborder Printer Configuration Tool'
Label(frame1, bg="lightblue", text=header).grid(row=0)
printers = commands.getoutput('lpstat -p')
printers = printers.split('\n')
#printers.remove('\tPaused')
printerNames = []
for printer in printers:
printer = printer.split(' ')
printerNames.append(printer[1])
if len(printerNames) > 4:
scrollPrinter = Scrollbar(frame1, orient=VERTICAL)
printerList = Listbox(frame1, yscrollcommand=scrollPrinter.set, selectmode=SINGLE, selectbackground="yellow", height=4)
scrollPrinter.config(command=printerList.yview)
scrollPrinter.grid(row=1, column=1, rowspan=4, sticky=N+S)
else:
printerList = Listbox(frame1, selectmode=SINGLE, selectbackground="yellow", height=4)
for printerName in printerNames:
printerList.insert(END, printerName)
printerList.grid(row=1, column=0, rowspan=4, sticky=E+W)
Button(frame2, highlightbackground="lightblue", text="Test", command=testPrinter).grid(row=0, column=0)
Button(frame2, highlightbackground="lightblue", text="Set", command=getPrinter).grid(row=0, column=1)
root.mainloop()
@DAZ-WAN1
Copy link

Hello Mr. Mutaku,

Thanks for the code on printer selection on tkinter .I manage to change the command module which you imported to subprocess after reading about it. Found it been changed to subprocess in python 3.6 version .Have run the script but what i got as an output is gui with two labels and buttons. I was expecting to get something like what you get in microsoft word when you click on print button ,which displays printer lists you've installed on your machine for you to print through.

I would be grateful if you can modify the code or direct me to a link which i can get how to do printer selection in tkinter . Have attached two images named Mr.Mutaka script and Expecting result.

Best regards.

mr mutaku script
expecting result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment