Нужно, чтобы при нажатии self.printAction открывалось окно MyWindow (чтобы в открывшемся окне функционировал принтер, который уже написан)
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow, \
QMenu, QToolBar,QTextEdit, QVBoxLayout, QPushButton
from PyQt5 import QtCore, QtWidgets, QtGui, QtPrintSupport
from PyQt5.Qt import *
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Python Menus&")
self.resize(400, 200)
self.centralWidget = QWidget(objectName='centralWidget')
self.setCentralWidget(self.centralWidget)
self.centralWidget.setStyleSheet("color: rgb(255, 90, 58); background-color: #105652; ")
self.text = QTextEdit('Hello! Write here!')
layout = QVBoxLayout(self.centralWidget)
layout.addWidget(self.text)
self._createActions()
self._createMenuBar()
def _createMenuBar(self):
menuBar = self.menuBar()
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
fileMenu.addAction(self.newAction)
fileMenu.addAction(self.openAction)
fileMenu.addAction(self.saveAction)
fileMenu.addAction(self.exitAction)
editMenu = menuBar.addMenu("&Edit")
editMenu.addAction(self.copyAction)
editMenu.addAction(self.pasteAction)
editMenu.addAction(self.cutAction)
helpMenu = menuBar.addMenu("&Help")
helpMenu.addAction(self.helpContentAction)
helpMenu.addAction(self.aboutAction)
printMenu = menuBar.addMenu("&Print")
printMenu.addAction(self.printAction)
def _createActions(self):
self.newAction = QAction(self)
self.newAction.setText("New")
self.openAction = QAction("Open", self)
self.saveAction = QAction("Save", self)
self.exitAction = QAction("Exit", self)
self.copyAction = QAction("&Copy", self)
self.pasteAction = QAction("&Paste", self)
self.cutAction = QAction("Cut", self)
self.helpContentAction = QAction("Help", self)
self.aboutAction = QAction("About", self)
self.printAction = QAction("Print", self)
#self.printAction.triggered.connect(self.show_window_1)
def show_window_1(self):
#app_1 = QApplication(sys.argv)
self.w1 = MyWindow()
self.w1.show()
#sys.exit(app_1.exec())
class MyWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent, flags=QtCore.Qt.Window |
QtCore.Qt.MSWindowsFixedSizeDialogHint)
self.setWindowTitle("Печать изображений")
def func(self):
self.setWindowTitle("Печать изображений")
self.printer = QtPrintSupport.QPrinter()
self.printer.setPageOrientation(QtGui.QPageLayout.Landscape)
self.file = None
vbox = QtWidgets.QVBoxLayout()
btnOpen = QtWidgets.QPushButton("&Открыть файл...")
btnOpen.clicked.connect(self.openFile)
vbox.addWidget(btnOpen)
btnPageOptions = QtWidgets.QPushButton("Настройка &страницы...")
btnPageOptions.clicked.connect(self.showPageOptions)
vbox.addWidget(btnPageOptions)
btnPreview = QtWidgets.QPushButton("П&росмотр...")
btnPreview.clicked.connect(self.preview)
vbox.addWidget(btnPreview)
btnPrint = QtWidgets.QPushButton("&Печать...")
btnPrint.clicked.connect(self.print)
vbox.addWidget(btnPrint)
self.setLayout(vbox)
self.resize(200, 100)
def showPageOptions(self):
pd = QtPrintSupport.QPageSetupDialog(self.printer, parent=self)
pd.exec()
def preview(self):
pp = QtPrintSupport.QPrintPreviewDialog(self.printer, parent=self)
pp.paintRequested.connect(self._printText)
pp.exec()
def print(self):
pd = QtPrintSupport.QPrintDialog(self.printer, parent=self)
pd.setOptions(
QtPrintSupport.QAbstractPrintDialog.PrintToFile | QtPrintSupport.QAbstractPrintDialog.PrintSelection)
if pd.exec() == QtWidgets.QDialog.Accepted:
self._printImage(self.printer)
self.show()
def _printText(self, printer):
painter = QtGui.QPainter()
painter.begin(printer)
color = QtGui.QColor(QtCore.Qt.black)
painter.setPen(QtGui.QPen(color))
painter.setBrush(QtGui.QBrush(color))
font = QtGui.QFont("Verdana", pointSize=42)
painter.setFont(font)
#pixmap = QtGui.QPixmap(self.file)
painter.drawText(printer.width(), printer.height(), QtCore.Qt.TextDontClip, self.text.text())
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
Ответы (2 шт):
Автор решения: Килобайт
→ Ссылка
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow, \\
QMenu, QToolBar,QTextEdit, QVBoxLayout, QPushButton
from PyQt5 import QtCore, QtWidgets, QtGui, QtPrintSupport
from PyQt5.Qt import *
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Python Menus&")
self.resize(400, 200)
self.centralWidget = QWidget(objectName='centralWidget')
self.setCentralWidget(self.centralWidget)
self.centralWidget.setStyleSheet("color: rgb(255, 90, 58); background-color: #105652; ")
self.text = QTextEdit('Hello! Write here!')
layout = QVBoxLayout(self.centralWidget)
layout.addWidget(self.text)
self._createActions()
self._createMenuBar()
def _createMenuBar(self):
menuBar = self.menuBar()
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
fileMenu.addAction(self.newAction)
fileMenu.addAction(self.openAction)
fileMenu.addAction(self.saveAction)
fileMenu.addAction(self.exitAction)
editMenu = menuBar.addMenu("&Edit")
editMenu.addAction(self.copyAction)
editMenu.addAction(self.pasteAction)
editMenu.addAction(self.cutAction)
helpMenu = menuBar.addMenu("&Help")
helpMenu.addAction(self.helpContentAction)
helpMenu.addAction(self.aboutAction)
printMenu = menuBar.addMenu("&Print")
printMenu.addAction(self.printAction)
def _createActions(self):
self.newAction = QAction(self)
self.newAction.setText("New")
self.openAction = QAction("Open", self)
self.saveAction = QAction("Save", self)
self.exitAction = QAction("Exit", self)
self.copyAction = QAction("&Copy", self)
self.pasteAction = QAction("&Paste", self)
self.cutAction = QAction("Cut", self)
self.helpContentAction = QAction("Help", self)
self.aboutAction = QAction("About", self)
self.printAction = QAction("Print", self)
self.printAction.triggered.connect(self.show_window_1)
def show_window_1(self):
self.w1 = MyWindow()
self.w1.show()
class MyWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent, flags=QtCore.Qt.Window |
QtCore.Qt.MSWindowsFixedSizeDialogHint)
self.setWindowTitle("Печать изображений")
self.text = QTextEdit('Type your text here')
self.button = QPushButton('Print', self)
self.button.clicked.connect(self.func)
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.text)
vbox.addWidget(self.button)
self.setLayout(vbox)
self.resize(200, 100)
def func(self):
self.setWindowTitle("Печать изображений")
self.printer = QtPrintSupport.QPrinter()
self.printer.setPageOrientation(QtGui.QPageLayout.Landscape)
self.file = None
self.preview()
def showPageOptions(self):
pd = QtPrintSupport.QPageSetupDialog(self.printer, parent=self)
pd.exec()
def preview(self):
pp = QtPrintSupport.QPrintPreviewDialog(self.printer, parent=self)
pp.paintRequested.connect(self._printText)
pp.exec()
def print(self):
pd = QtPrintSupport.QPrintDialog(self.printer, parent=self)
pd.setOptions(
QtPrintSupport.QAbstractPrintDialog.PrintToFile | QtPrintSupport.QAbstractPrintDialog.PrintSelection)
if pd.exec() == QtWidgets.QDialog.Accepted:
self._printImage(self.printer)
self.show()
def _printText(self, printer):
painter = QtGui.QPainter()
painter.begin(printer)
color = QtGui.QColor(QtCore.Qt.black)
painter.setPen(QtGui.QPen(color))
painter.setBrush(QtGui.QBrush(color))
font = QtGui.QFont("Verdana", pointSize=12)
painter.setFont(font)
painter.drawText(printer.width(), printer.height(), QtCore.Qt.TextDontClip, self.text.toPlainText())
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
Автор решения: S. Nick
→ Ссылка
Не уверен, что правильно понял вас, но попробуйте так:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui, QtPrintSupport
from PyQt5.Qt import *
class MyWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent, # +
flags=Qt.Window | Qt.MSWindowsFixedSizeDialogHint)
self.parent = parent # +++
self.setWindowTitle("Печать изображений")
self.func() # +++
def func(self):
self.setWindowTitle("Печать изображений")
self.printer = QtPrintSupport.QPrinter()
self.printer.setPageOrientation(QtGui.QPageLayout.Landscape)
self.file = None
vbox = QtWidgets.QVBoxLayout()
btnOpen = QtWidgets.QPushButton("&Открыть файл...")
btnOpen.clicked.connect(self.openFile) # ???
vbox.addWidget(btnOpen)
btnPageOptions = QtWidgets.QPushButton("Настройка &страницы...")
btnPageOptions.clicked.connect(self.showPageOptions)
vbox.addWidget(btnPageOptions)
btnPreview = QtWidgets.QPushButton("П&росмотр...")
btnPreview.clicked.connect(self.preview)
vbox.addWidget(btnPreview)
btnPrint = QtWidgets.QPushButton("&Печать...")
btnPrint.clicked.connect(self._print)
vbox.addWidget(btnPrint)
self.setLayout(vbox)
self.resize(200, 100)
def openFile(self): # ???
pass
def showPageOptions(self):
pd = QtPrintSupport.QPageSetupDialog(self.printer, parent=self)
pd.exec()
def preview(self):
pp = QtPrintSupport.QPrintPreviewDialog(self.printer, parent=self)
pp.paintRequested.connect(self._printText)
pp.exec()
def _print(self):
pd = QtPrintSupport.QPrintDialog(self.printer, parent=self)
pd.setOptions(
QtPrintSupport.QAbstractPrintDialog.PrintToFile | QtPrintSupport.QAbstractPrintDialog.PrintSelection)
if pd.exec() == QtWidgets.QDialog.Accepted:
self._printImage(self.printer)
self.show()
def _printText(self, printer):
painter = QtGui.QPainter()
painter.begin(printer)
color = QtGui.QColor(QtCore.Qt.black)
painter.setPen(QtGui.QPen(color))
painter.setBrush(QtGui.QBrush(color))
font = QtGui.QFont("Verdana", pointSize=42)
painter.setFont(font)
#pixmap = QtGui.QPixmap(self.file)
painter.drawText(
# printer.width(),
# printer.height(),
# QtCore.Qt.TextDontClip,
# self.text.text()
42, # +++
42, # +++
self.parent.text.toPlainText() # +++
)
painter.end()
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Python Menus&")
self.resize(400, 200)
self.centralWidget = QWidget(objectName='centralWidget')
self.setCentralWidget(self.centralWidget)
self.centralWidget.setStyleSheet("color: rgb(255, 90, 58); background-color: #105652; ")
self.text = QTextEdit('Hello! Write here!')
layout = QVBoxLayout(self.centralWidget)
layout.addWidget(self.text)
self._createActions()
self._createMenuBar()
def _createMenuBar(self):
menuBar = self.menuBar()
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
fileMenu.addAction(self.newAction)
fileMenu.addAction(self.openAction)
fileMenu.addAction(self.saveAction)
fileMenu.addAction(self.exitAction)
editMenu = menuBar.addMenu("&Edit")
editMenu.addAction(self.copyAction)
editMenu.addAction(self.pasteAction)
editMenu.addAction(self.cutAction)
helpMenu = menuBar.addMenu("&Help")
helpMenu.addAction(self.helpContentAction)
helpMenu.addAction(self.aboutAction)
printMenu = menuBar.addMenu("&Print")
printMenu.addAction(self.printAction)
def _createActions(self):
self.newAction = QAction(self)
self.newAction.setText("New")
self.openAction = QAction("Open", self)
self.saveAction = QAction("Save", self)
self.exitAction = QAction("Exit", self)
self.copyAction = QAction("&Copy", self)
self.pasteAction = QAction("&Paste", self)
self.cutAction = QAction("Cut", self)
self.helpContentAction = QAction("Help", self)
self.aboutAction = QAction("About", self)
self.printAction = QAction("Print", self)
self.printAction.triggered.connect(self.show_window_1)
def show_window_1(self):
self.w1 = MyWindow(self) # +++ (self)
self.w1.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
