打印机部分由win32编写,Mac系统通通注释掉,哈哈哈,我也没测可以不可以用,学长给的函数
Widget.py
from PyQt5.QtGui import QFont, QPixmapfrom PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QComboBox, QGridLayout, QPushButton, QTableWidget, \ QAbstractItemView, QRadioButton# import win32print as PrinterOperaterclass UI(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):grid = QGridLayout()grid.setSpacing(10)label1 = QLabel(self)label2 = QLabel(self)label3 = QLabel(self)font1 = QFont("Arial", 17, QFont.Black)label1.setText('系统当前默认打印机:')label1.setFont(font1)label2.setText('修改系统默认打印机:')label2.setFont(font1)label1_edit = QLineEdit(self)label1_edit.setReadOnly(True)# allPrinters = PrinterOperater.EnumPrinters(2)label2_edit = QComboBox(self)# label2_edit.addItem(allPrinters)font2 = QFont("Arial", 14, QFont.Black)btn1 = QPushButton(self)btn2 = QPushButton(self)btn3 = QPushButton(self)btn1.setFont(font2)btn2.setFont(font2)btn3.setFont(font2)btn1.setText('获取系统所有可用打印机')btn2.setText('选取当前打印机为默认打印机')btn3.setText("刷新打印列表")# btn1.clicked.connect(self.getAllPrinters)# btn2.clicked.connect(self.getDefaultPrinter)# btn3.clicked.connect(self.getAllJobs)Table = QTableWidget(20, 5)Row_name = ['任务编号','任务名称','文件页数','打印状态','备注',]Table.setHorizontalHeaderLabels(Row_name)Table.setEditTriggers(QAbstractItemView.NoEditTriggers)image = QPixmap('images/image.png')label3.setPixmap(image)CheckBox1 = QRadioButton('上线营业')CheckBox2 = QRadioButton('暂停营业')CheckBox1.setChecked(True)CheckBox1.toggled.connect(lambda: self.btnstate(CheckBox1))CheckBox1.toggled.connect(lambda: self.btnstate(CheckBox2))grid.addWidget(label1, 1, 0)grid.addWidget(label1_edit, 1, 1)grid.addWidget(btn1, 1, 2, 1, 2)grid.addWidget(label2, 2, 0)grid.addWidget(label2_edit, 2, 1)grid.addWidget(btn2, 2, 2, 1, 2)grid.addWidget(btn3, 3, 2, 1, 2)grid.addWidget(Table, 3, 0, 5, 2)grid.addWidget(label3, 5, 2)grid.addWidget(CheckBox1, 6, 2)grid.addWidget(CheckBox2, 7, 2)self.setLayout(grid)def btnstate(self, btn):# 输出按钮1与按钮2的状态,选中还是没选中if btn.text() == '上线营业':if btn.isChecked() == True:print(btn.text() + "is selected")else:print(btn.text() + "is deselected")if btn.text() == "暂停营业":if btn.isChecked() == True:print(btn.text() + "is selected")else:print(btn.text() + "is deselected")# def getDefaultPrinter(self):# defaultPrinter = PrinterOperater.GetDefaultPrinter()## print(defaultPrinter)## def getAllJobs(self):# defaultPrinter = PrinterOperater.GetDefaultPrinter()# PyPrinterHANDLE = PrinterOperater.OpenPrinter(defaultPrinter)# allPrintJobs = PrinterOperater.EnumJobs(PyPrinterHANDLE, 0, 15, 1)## print(allPrintJobs)## def setDefaultPrinter(self):# tempPrinter = sys.argv[1];## PrinterOperater.SetDefaultPrinter(tempPrinter)## defaultPrinter = PrinterOperater.GetDefaultPrinter()## print(defaultPrinter)
MainWindow.py
from PyQt5.QtWidgets import QDesktopWidget, QMessageBoxfrom Widget import UIclass MainWindow(UI):def __init__(self):super().__init__()self.showWindow()def showWindow(self):self.resize(720, 640)self.setWindowTitle('云打印服务系统(商家版)-桌面版')self.center()self.show()def center(self):qr = self.frameGeometry()cp = QDesktopWidget().availableGeometry().center()qr.moveCenter(cp)self.move(qr.topLeft())def closeEvent(self, QCloseEvent):reply = QMessageBox.question(self, 'Message', "你确定退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)if reply == QMessageBox.Yes:QCloseEvent.accept()else:QCloseEvent.ignore()
Main.py
import sysfrom PyQt5.QtWidgets import QApplicationfrom MainWindow import MainWindowif __name__ == '__main__':app = QApplication(sys.argv)Window = MainWindow()sys.exit(app.exec_())