2017-09-04 117 views
0

我想在計算機上選擇一個已安裝的打印機並通過它進行打印,但是我的組合框不顯示我機器上的打印機,而是在我的IDE中打印到我的終端。如何通過tkinter中的本地或網絡打印機進行打印

已經閱讀下面it.This我的代碼後,在解決辦this.Have試圖到達此數天安裝win32print模塊:

from tkinter import * 
from tkinter import ttk 
import win32print 



def installed_printer(): 
    printers = win32print.EnumPrinters(2) 
     for p in printers: 
     return(p) 



def locprinter(): 
    pt = Toplevel() 
    pt.geometry("250x250") 
    pt.title("choose printer") 
    LABEL = Label(pt, text="select Printer").pack() 
    PRCOMBO = ttk.Combobox(pt, width=35, 
    textvariable=installed_printer).pack() 
    BUTTON = ttk.Button(pt, text="refresh", 
    command=installed_printer).pack() 


root = Tk() 
root.title("printer selection in tkinter") 
root.geometry("400x400") 


menubar = Menu(root) 
root.config(menu=menubar) 

file_menu = Menu(menubar) 
menubar.add_cascade(label="File", menu=file_menu) 
file_menu.add_command(label="printer", command=locprinter) 


LAB = Label(root, text="Comment") 
T2 = Text(root, width=40, height=10) 


def INFO(): 
print(T2.get("1.0", END)) 


Print_Button = Button(root, text ="Print", command = 
INFO).place(x=180,y=250) 

LAB.pack() 
T2.pack() 

root.mainloop() 

我怎樣才能做到這一點,我想用tkinter框架在我的文本框中打印內容。

回答

0

這是你想要創建的嗎?使用此代碼,我將打印機列入組合框,將文本框中的內容放入tempfile,然後通過win32api和win32print python模塊打印內容。它在我嘗試過的3臺電腦上工作。

from tkinter import * 
from tkinter import ttk 
import win32api 
import win32print 
import tempfile 



def installed_printer(): 
    printers = win32print.EnumPrinters(2) 
    for p in printers: 
     return(p) 

printerdef = '' 

def locprinter(): 
    pt = Toplevel() 
    pt.geometry("250x250") 
    pt.title("choose printer") 
    var1 = StringVar() 
    LABEL = Label(pt, text="select Printer").pack() 
    PRCOMBO = ttk.Combobox(pt, width=35,textvariable=var1) 
    print_list = [] 
    printers = list(win32print.EnumPrinters(2)) 
    for i in printers: 
     print_list.append(i[2]) 
    print(print_list) 
    # Put printers in combobox 
    PRCOMBO['values'] = print_list 
    PRCOMBO.pack() 
    def select(): 
     global printerdef 
     printerdef = PRCOMBO.get() 
     pt.destroy() 
    BUTTON = ttk.Button(pt, text="Done",command=select).pack() 

root = Tk() 
root.title("printer selection in tkinter") 
root.geometry("400x400") 


menubar = Menu(root) 
root.config(menu=menubar) 

file_menu = Menu(menubar) 
menubar.add_cascade(label="File", menu=file_menu) 
file_menu.add_command(label="printer", command=locprinter) 


LAB = Label(root, text="Comment") 
T2 = Text(root, width=40, height=10) 


def INFO(): 
    printText = T2.get("1.0", END) 
    print(printText) 
    print(printerdef) 
    filename = tempfile.mktemp(".txt") 
    open(filename, "w").write(printText) 
    # Bellow is call to print text from T2 textbox 
    win32api.ShellExecute(
     0, 
     "printto", 
     filename, 
     '"%s"' % win32print.GetDefaultPrinter(), 
     ".", 
     0 
    ) 


Print_Button = Button(root, text ="Print", command=INFO).place(x=180,y=250) 

LAB.pack() 
T2.pack() 

root.mainloop() 

編輯:下面的代碼,這個錯誤 What I am getting as an error

+0

能否請你解釋的差異,爲什麼你做這些改變的記錄? – NatNgs

+0

使用上面的代碼將安裝的打印機輸出到combox,但是當我輸入內容並希望打印選定的prrinter不打印內容時,請評論您的代碼。 –

+0

頁面小到打印嘗試使用較小的字體。 –

相關問題