adds52339 发表于 2021-11-28 18:01:03

pyqt5 打印机问题

# -*- coding: utf-8 -*-

# import picture_rc
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from PyQt5.QtGui import QIcon, QPixmap, QImage
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog, QPageSetupDialog

class Ui_MainWindow(object):
    def __init__(self):
      super(Ui_MainWindow, self).__init__()
      self.printer = QPrinter()#定义一个默认的打印机
      self.setupUi(MainWindow)

    def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
      MainWindow.resize(479, 626)
      self.centralwidget = QtWidgets.QWidget(MainWindow)
      self.centralwidget.setObjectName("centralwidget")
      self.settingbutton = QtWidgets.QPushButton(self.centralwidget)
      self.settingbutton.setGeometry(QtCore.QRect(290, 540, 121, 31))
      self.settingbutton.setStyleSheet("")
      self.settingbutton.setObjectName("pushButton")
      self.printbutton = QtWidgets.QPushButton(self.centralwidget)
      self.printbutton.setGeometry(QtCore.QRect(160, 540, 121, 31))
      self.printbutton.setStyleSheet("")
      self.printbutton.setObjectName("pushButton_4")
      self.listView = QtWidgets.QListView(self.centralwidget)
      self.listView.setGeometry(QtCore.QRect(-10, -10, 1001, 641))
      self.listView.setLayoutDirection(QtCore.Qt.LeftToRight)
      self.listView.setStyleSheet("background-image: url(./image/beijing.png);")
      self.listView.setObjectName("listView")
      self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
      self.lineEdit_5.setGeometry(QtCore.QRect(190, 290, 111, 31))
      self.lineEdit_5.setStyleSheet("font: 14pt \"楷体\";")
      self.lineEdit_5.setText("")
      self.lineEdit_5.setObjectName("lineEdit_5")
      self.label_6 = QtWidgets.QLabel(self.centralwidget)
      self.label_6.setGeometry(QtCore.QRect(80, 50, 311, 461))
      self.label_6.setText("")
      self.label_6.setPixmap(QtGui.QPixmap("./image/gongpai.png"))
      self.label_6.setScaledContents(True)
      self.label_6.setObjectName("label_6")
      self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
      self.lineEdit_6.setGeometry(QtCore.QRect(190, 340, 111, 31))
      self.lineEdit_6.setStyleSheet("font: 12pt \"黑体\";")
      self.lineEdit_6.setText("输入部门")
      self.lineEdit_6.setObjectName("lineEdit_6")
      self.lineEdit_7 = QtWidgets.QLineEdit(self.centralwidget)
      self.lineEdit_7.setGeometry(QtCore.QRect(190, 380, 111, 31))
      self.lineEdit_7.setStyleSheet("font: 12pt \"黑体\";")
      self.lineEdit_7.setText("输入职务")
      self.lineEdit_7.setObjectName("lineEdit_7")
      self.lineEdit_8 = QtWidgets.QLineEdit(self.centralwidget)
      self.lineEdit_8.setGeometry(QtCore.QRect(190, 430, 111, 31))
      self.lineEdit_8.setLayoutDirection(QtCore.Qt.LeftToRight)
      self.lineEdit_8.setStyleSheet("font: 12pt \"黑体\";")
      self.lineEdit_8.setText("输入工号")
      self.lineEdit_8.setCursorPosition(0)
      self.lineEdit_8.setObjectName("lineEdit_8")
      self.listView.raise_()
      self.settingbutton.raise_()
      self.printbutton.raise_()
      self.label_6.raise_()
      self.lineEdit_5.raise_()
      self.lineEdit_6.raise_()
      self.lineEdit_7.raise_()
      self.lineEdit_8.raise_()
      MainWindow.setCentralWidget(self.centralwidget)

      self.retranslateUi(MainWindow)
      QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
      _translate = QtCore.QCoreApplication.translate
      MainWindow.setWindowTitle(_translate("MainWindow", "工牌打印"))
      self.settingbutton.setText(_translate("MainWindow", "打印设置"))
      self.printbutton.setText(_translate("MainWindow", "打印工牌"))

      self.settingbutton.clicked.connect(self.showsettingdailog)
      self.printbutton.clicked.connect(self.showprintdialog)

    # 显示打印机设置
    def showsettingdailog(self):
      printerdailog = QPageSetupDialog(self.printer)
      printerdailog.exec()

    # 显示打印对话框
    def showprintdialog(self):
      print = QPrintDialog(self.printer)
      if QDialog.Accepted == print.exec():
            self.label_6.print(self.printer)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./image/w1.png'))
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

以上是完整代码。

    运行没问题,功能没问题,就是点打印,自动关闭,然后弹出      (进程已结束,退出代码为 -1073740791 (0xC0000409))    打印机也没反应。

求助,是哪里错了?为什么打印之后,不会出来?还会直接关闭。
页: [1]
查看完整版本: pyqt5 打印机问题