我有一个问题,但这次它与 Wxpython 的关系比与 Tkinter 的关系更多。我通常不使用这个模块,所以我对它知之甚少。在以下代码中,在 Tkinter 测试窗口中按 Enter 键将打开 windows 打印窗口。如果发送以打印为 PDF,它可以完美运行。但是如果重复这个过程,就会出现错误。下面是测试代码和错误。
代码
from threading import Thread
from tkinter import Tk
import wx
def f_imprimir(codigo):
class TextDocPrintout(wx.Printout):
def __init__(self):
wx.Printout.__init__(self)
def OnPrintPage(self, page):
dc = self.GetDC()
ppiPrinterX, ppiPrinterY = self.GetPPIPrinter()
ppiScreenX, ppiScreenY = self.GetPPIScreen()
logScale = float(ppiPrinterX)/float(ppiScreenX)
pw, ph = self.GetPageSizePixels()
dw, dh = dc.GetSize()
scale = logScale * float(dw)/float(pw)
dc.SetUserScale(scale, scale)
logUnitsMM = float(ppiPrinterX)/(logScale*25.4)
codigo(dc, logUnitsMM)
return True
class PrintFrameworkSample(wx.Frame):
def OnPrint(self):
pdata = wx.PrintData()
pdata.SetPaperId(wx.PAPER_A4)
pdata.SetOrientation(wx.LANDSCAPE)
data = wx.PrintDialogData(pdata)
printer = wx.Printer(data)
printout = TextDocPrintout()
useSetupDialog = True
if not printer.Print(self, printout, useSetupDialog) and printer.GetLastError() == wx.PRINTER_ERROR:
wx.MessageBox(
"There was a problem printing.\n"
"Perhaps your current printer is not set correctly?",
"Printing Error", wx.OK)
else:
data = printer.GetPrintDialogData()
printout.Destroy()
self.Destroy()
app=wx.App(False)
PrintFrameworkSample().OnPrint()
def funcion(dc, MM):
dc.DrawText("hola mundo", MM*16, MM*73)
def imprimir(codigo):
t = Thread(target=f_imprimir, args=(codigo,))
t.start()
Tk().bind("<Return>", lambda Event:imprimir(funcion))
错误
File "C:\Users\DANTE\Google Drive\JNAAB\DESARROLLO\pruebas\t\s\prueba.py", line 11, in OnPrintPage
dc = self.GetDC()
AttributeError: 'TextDocPrintout' object has no attribute 'GetDC'
有谁知道问题的解决方案?谢谢你。