Top.Mail.Ru
Ответы

Помощь по питону!!!PyQt5!!!!

Добавить в блокнот из прошлого задания возможность предварительного просмотра текста, а также сохранения в pdf
ВОТ КОД!:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
 
import sys 
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QTextEdit, QAction 
 
 
class MainWindow(QMainWindow): 
    def __init__(self): 
        super().__init__() 
        self.initUI() 
 
    def initUI(self): 
        self.setGeometry(100, 100, 500, 500) 
        self.setWindowTitle('Распечатать текст') 
 
        # виджет для отображения текста 
        self.text_edit = QTextEdit(self) 
        self.text_edit.setReadOnly(True) 
        self.setCentralWidget(self.text_edit) 
 
        # меню для открытия файла 
        open_file_action = QAction('Открыть файл', self) 
        open_file_action.setShortcut('Ctrl+O') 
        open_file_action.triggered.connect(self.open_file) 
        file_menu = self.menuBar().addMenu('Файл') 
        file_menu.addAction(open_file_action) 
 
        # кнопка для печати текста 
        print_action = QAction('Печать', self) 
        print_action.setShortcut('Ctrl+P') 
        print_action.triggered.connect(self.print_text) 
        self.toolbar = self.addToolBar('Печать') 
        self.toolbar.addAction(print_action) 
 
    def open_file(self): 
        # диалоговое окно для открытия файла 
        filename, _ = QFileDialog.getOpenFileName(self, 'Открыть файл', '', 'Текстовые файлы (*.txt)') 
        if filename: 
            # считываем содержимое файла и отображаем его в виджете 
            with open(filename, 'r', encoding='utf-8') as file: 
                self.text_edit.setText(file.read()) 
 
    def print_text(self): 
        # печать содержимого виджета 
        printer = QPrinter() 
        dialog = QPrintDialog(printer, self) 
        if dialog.exec_() == QDialog.Accepted: 
            self.text_edit.print_(printer) 
 
 
if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    window = MainWindow() 
    window.show() 
    sys.exit(app.exec_()) 
По дате
По рейтингу
Аватар пользователя

попроси чат



Видео по теме