gpt4 book ai didi

python - 如何在 tkinter 中通过本地或网络打印机进行打印

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:48 26 4
gpt4 key购买 nike

我想在我的计算机上选择一台已安装的打印机并通过它打印,但我的组合框不显示我机器上的打印机,而是打印到我的 IDE 中的终端。

已经尝试了好几天,并找到了解决方案。在阅读之后安装了 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 框架打印我的文本框中的内容。

最佳答案

这是您想要创建的吗?

    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, wrap=WORD)


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()

关于python - 如何在 tkinter 中通过本地或网络打印机进行打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46041227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com