Beispiel #1
0
class GUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(600, 600)
        self.setWindowTitle("Untitled-Notepad")
        self.setWindowIcon(
            QIcon(r'C:\Users\Yash\PycharmProjects\ppppp\Notepad\icons.png'))
        #self.timer=QTimer(self)
        #self.timer.timeout.connect(self.geometrychange)
        #self.timer.start(10)
        self.menu = QMenuBar(self)
        self.file = QMenu("File", self.menu)
        file = self.file.addAction("New")
        file.setShortcut("Ctrl+N")
        file.triggered.connect(self.files)
        opens = self.file.addAction('Open')
        opens.setShortcut("Ctrl+O")
        opens.triggered.connect(self.open)
        save = self.file.addAction("Save")
        save.setShortcut("Ctrl+S")
        save.triggered.connect(self.save)
        saveas = self.file.addAction("Save as")
        saveas.triggered.connect(self.saveas)
        self.file.addSeparator()
        printsetup = self.file.addAction("Print Preview")
        printsetup.triggered.connect(self.printsetup)
        prints = self.file.addAction("Print")
        prints.setShortcut("Ctrl+P")
        prints.triggered.connect(self.prints)
        self.file.addSeparator()
        exits = self.file.addAction("Exit")
        exits.setShortcut("Alt+X")
        exits.triggered.connect(self.exists)
        self.menu.addMenu(self.file)

        self.edit = QMenu("Edit", self.menu)
        undo = self.edit.addAction("Undo")
        undo.setShortcut("Ctrl+Z")
        undo.triggered.connect(self.undos)
        self.edit.addSeparator()
        self.cuts = self.edit.addAction("Cut")
        self.cuts.setShortcut("Ctrl+X")
        self.cuts.triggered.connect(self.cut)
        self.copys = self.edit.addAction("Copy")
        self.copys.setShortcut("Ctrl+C")
        self.copys.triggered.connect(self.copy)
        self.pastes = self.edit.addAction("Paste")
        self.pastes.setShortcut("Ctrl+V")
        self.pastes.triggered.connect(self.paste)
        self.deletes = self.edit.addAction("Delete")
        self.deletes.setShortcut("Del")
        self.deletes.triggered.connect(self.delete)
        self.edit.addSeparator()
        self.finding = self.edit.addAction("Find")
        self.finding.setShortcut("Ctrl+F")
        self.finding.triggered.connect(self.finds)
        self.findnexts = self.edit.addAction("Find Next")
        self.findnexts.setShortcut("F3")
        self.findnexts.triggered.connect(self.nexts)
        replace = self.edit.addAction("Replace")
        replace.setShortcut("Ctrl+H")
        replace.triggered.connect(self.replace)
        go = self.edit.addAction("Go To")
        go.setShortcut("Ctrl+G")
        go.triggered.connect(self.go)
        check = QTimer(self)
        check.timeout.connect(self.check)
        check.start(100)
        self.edit.addSeparator()
        select = self.edit.addAction("Select All")
        select.setShortcut("Ctrl+A")
        select.triggered.connect(self.select)
        time = self.edit.addAction("Time/Date")
        time.setCheckable(True)
        time.setShortcut("F5")
        time.triggered.connect(self.todaytime)
        self.menu.addMenu(self.edit)

        self.format = QMenu("Format", self.menu)
        self.wrap = self.format.addAction("Word Wrap")
        self.wrap.setCheckable(True)
        self.wrap.setChecked(True)
        self.wrap.triggered.connect(self.word)
        font = self.format.addAction("Font")
        font.triggered.connect(self.fonts)
        self.menu.addMenu(self.format)

        self.view = QMenu("View", self.menu)
        zoom = QMenu("Zoom", self.view)
        In = zoom.addAction("Zoom In")
        In.setShortcut("Ctrl++")
        In.triggered.connect(self.zoomin)
        out = zoom.addAction("Zoom Out")
        out.setShortcut("Ctrl+-")
        out.triggered.connect(self.zoomout)
        restore = zoom.addAction("Restore Default Zoom")
        restore.setShortcut("Ctrl+0")
        restore.triggered.connect(self.restore)
        self.view.addMenu(zoom)
        self.statused = self.view.addAction("Status Bar")
        self.statused.setCheckable(True)
        self.statused.setChecked(True)
        self.statused.triggered.connect(self.status)
        self.menu.addMenu(self.view)

        self.help = QMenu("Help", self.menu)
        help = self.help.addAction("View Help")
        help.triggered.connect(self.helps)
        self.help.addSeparator()
        about = self.help.addAction("About Notepad")
        about.triggered.connect(self.about)
        self.menu.addMenu(self.help)
        self.setMenuBar(self.menu)

        self.centralwidget = QWidget(self)
        self.textEdit = QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QRect(0, 0, 600, 579))
        self.textEdit.setUndoRedoEnabled(True)
        font = QFont()
        font.setFamily('Arial')
        font.setPointSize(16)
        self.textEdit.setFont(font)
        self.textEdit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.textEdit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.textEdit.setLineWrapMode(self.textEdit.WidgetWidth)
        self.textEdit.setLineWidth(2)
        try:
            self.path = str(sys.argv[1])
            f = open(self.path, 'r', encoding='utf-8')
            data = f.read()
            self.textEdit.setText(data)
            self.setWindowTitle(os.path.basename(self.path) + '-Notepad')
        except:
            pass
        self.setCentralWidget(self.centralwidget)
        self.show()

    def closeEvent(self, event):
        if self.close:
            reply = QMessageBox.question(self, "You want to quit? ",
                                         "Task is in progress !",
                                         QMessageBox.Yes, QMessageBox.No)
            if reply == QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()

    def resizeEvent(self, event):
        if self.statused.isChecked() == True:
            self.status()
            self.statusbar.setGeometry(
                QRect(self.geometry().x() - self.geometry().getCoords()[0],
                      self.geometry().width(),
                      self.geometry().width(), 20))
            self.textEdit.setGeometry(
                QRect(self.geometry().x() - self.geometry().getCoords()[0],
                      self.geometry().y() - self.geometry().getCoords()[1],
                      self.geometry().width(),
                      self.geometry().height() - 42))
        else:
            self.textEdit.setGeometry(
                QRect(self.geometry().x() - self.geometry().getCoords()[0],
                      self.geometry().y() - self.geometry().getCoords()[1],
                      self.geometry().width(),
                      self.geometry().height() - 21))

    def check(self):
        if len(self.textEdit.toPlainText()) == 0:
            self.copys.setDisabled(True)
            self.cuts.setDisabled(True)
            self.pastes.setDisabled(True)
            self.deletes.setDisabled(True)
            self.finding.setDisabled(True)
            self.findnexts.setDisabled(True)
        else:
            self.finding.setDisabled(False)
            self.findnexts.setDisabled(False)
            self.copys.setDisabled(False)
            self.cuts.setDisabled(False)
            self.pastes.setDisabled(False)
            self.deletes.setDisabled(False)
        if self.statused.isChecked() == True:
            time = QTime.currentTime()
            text = time.toString("hh:mm:ss")
            self.time.display(text)
            cursor = self.textEdit.textCursor()
            row = cursor.blockNumber() + 1
            col = cursor.columnNumber()
            self.label.setText("Row: " + str(row) + "| Col: " + str(col))

    def status(self):
        if self.statused.isChecked() == True:
            self.statusbar = QStatusBar(self)
            self.statusbar.setGeometry(
                QRect(self.geometry().x() - self.geometry().getCoords()[0],
                      self.geometry().width(),
                      self.geometry().width(), 20))
            self.textEdit.setGeometry(
                QRect(self.geometry().x() - self.geometry().getCoords()[0],
                      self.geometry().y() - self.geometry().getCoords()[1],
                      self.geometry().width(),
                      self.geometry().height() - 42))
            font = QFont()
            font.setPointSize(10)
            self.statusbar.setFont(font)
            self.label = QLabel("Row: 0 | Col: 0")
            self.statusbar.addPermanentWidget(self.label)
            self.time = QLCDNumber()
            self.time.setDigitCount(8)
            self.time.setFrameShadow(QFrame.Sunken)
            self.time.setFrameShape(QFrame.Panel)
            self.statusbar.addWidget(self.time)
            self.setStatusBar(self.statusbar)
        else:
            self.textEdit.setGeometry(
                QRect(self.geometry().x() - self.geometry().getCoords()[0],
                      self.geometry().y() - self.geometry().getCoords()[1],
                      self.geometry().width(),
                      self.geometry().height() - 21))

            vboxlayout = QVBoxLayout()
            vboxlayout.setContentsMargins(QtCore.QMargins())
            vboxlayout.setSpacing(0)
            sizegrip = QSizeGrip(self.centralwidget)
            sizegrip.setVisible(True)
            vboxlayout.addWidget(sizegrip, 0, Qt.AlignBottom | Qt.AlignRight)
            self.centralwidget.setLayout(vboxlayout)
            self.statusbar.hide()

    def helps(self):
        QMessageBox.information(self, "Help",
                                "This is your notepad made by PyQt5")

    def about(self):
        QMessageBox.information(self, "About",
                                "This is the Notepad made by Yash Rajput")

    def files(self):
        self.textEdit.setText('')
        self.setWindowTitle("Untitled-Notepad")
        self.filename = ["Untitled-Notepad"]
        self.new = True

    def open(self):
        self.filename = QFileDialog.getOpenFileName(
            self, 'Open File', '\home', 'Text Files (*.txt);;All Files (*)')
        if self.filename[0]:
            f = open(self.filename[0], 'r', encoding='utf-8')
        try:
            data = f.read()
            self.textEdit.setText(data)
            self.setWindowTitle(
                os.path.basename(self.filename[0]) + '-Notepad')
        except:
            pass

    def save(self):
        try:
            if self.new == False:
                w = open(self.filename[0], 'w')
                print(self.filename[0])
                w.write(self.textEdit.toPlainText())
                w.close()
            elif self.new == True:
                self.saveas()
        except:
            self.saveas()

    def saveas(self):
        self.filename = QFileDialog.getSaveFileName(
            self, "Save as", '\home', 'Text Files(*.txt);;All Files(*)')
        try:
            f = open(self.filename[0], 'w')
            f.write(self.textEdit.toPlainText())
            f.close()
            self.new = False
            self.setWindowTitle(
                os.path.basename(self.filename[0]) + '-Notepad')
        except:
            pass

    def exists(self):
        box = QMessageBox.question(self, "You want to quit? ",
                                   "Task is in progress !", QMessageBox.Yes,
                                   QMessageBox.No)
        if box.exec() == box.Yes:
            self.destroy()

    def undos(self):
        self.textEdit.undo()

    def cut(self):
        self.textEdit.cut()

    def copy(self):
        self.textEdit.copy()

    def paste(self):
        self.textEdit.paste()

    def select(self):
        self.textEdit.selectAll()

    def delete(self):
        cursor = self.textEdit.textCursor()
        cursor.removeSelectedText()

    def todaytime(self):
        self.textEdit.insertPlainText(str(datetime.today()))

    def word(self):
        if self.wrap.isChecked():
            self.textEdit.setLineWrapMode(self.textEdit.WidgetWidth)
        else:
            self.textEdit.setLineWrapMode(self.textEdit.NoWrap)

    def fonts(self):
        font, ok = QFontDialog.getFont()
        if ok:
            self.textEdit.setFont(font)

    def zoomin(self):
        font = QFont(self.textEdit.font())
        size = font.pointSize()
        font.setPointSize(size + 1)
        self.textEdit.setFont(font)

    def zoomout(self):
        font = QFont(self.textEdit.font())
        size = font.pointSize()
        font.setPointSize(size - 1)
        self.textEdit.setFont(font)

    def restore(self):
        font = QFont()
        font.setPointSize(8)
        font.setFamily('Arial')
        self.textEdit.setFont(font)

    def finds(self):
        self.pc = Find(w)
        self.pc.exec()

    def replace(self):
        p = replaces(w)
        p.exec()

    def go(self):
        temp = Go(w)
        temp.exec()

    def prints(self):
        self.printer = QPrinter(QPrinter.HighResolution)
        dialog = QPrintDialog(self.printer, self)
        if dialog.exec_() == QPrintDialog.Accepted:
            w.textEdit.print_(self.printer)

    def printsetup(self):
        self.printer = QPrinter(QPrinter.HighResolution)
        dialog = QPrintPreviewDialog(self.printer, self)
        dialog.paintRequested.connect(self.handle_paint_request)
        dialog.exec_()

    def handle_paint_request(self, printer):
        self.textEdit.print(printer)

    def nexts(self):
        self.pc.click()
Beispiel #2
0
class Ui_MainWindow(object):
    
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(784, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        
        self.Time = QLCDNumber(self.centralwidget)
        self.Time.setGeometry(QRect(260, 80, 271, 121))
        self.Time.setFrameShape(QFrame.Box)
        self.Time.setFrameShadow(QFrame.Sunken)
        self.Time.setLineWidth(1)
        self.Time.setMidLineWidth(0)
        self.Time.setSmallDecimalPoint(False)
        self.Time.setDigitCount(8)
        self.Time.setObjectName("lcdNumber")
        
        self.label = QLabel(self.centralwidget)
        self.label.setGeometry(QRect(220, 0, 341, 61))
        font = QFont()
        font.setBold(False)
        font.setWeight(50)
        self.label.setFont(font)
        self.label.setFocusPolicy(Qt.NoFocus)
        self.label.setFrameShape(QFrame.NoFrame)
        self.label.setTextFormat(Qt.AutoText)
        self.label.setScaledContents(False)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setIndent(-1)
        self.label.setObjectName("label")
        
        self.line = QFrame(self.centralwidget)
        self.line.setGeometry(QRect(0, 40, 831, 31))
        self.line.setFrameShape(QFrame.HLine)
        self.line.setFrameShadow(QFrame.Sunken)
        self.line.setObjectName("line")
        self.line_2 = QFrame(self.centralwidget)
        self.line_2.setGeometry(QRect(0, 220, 801, 31))
        self.line_2.setFrameShape(QFrame.HLine)
        self.line_2.setFrameShadow(QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        
        self.Melody = QPushButton(self.centralwidget)
        self.Melody.setGeometry(QRect(-270, 240, 1091, 61))
        self.Melody.setObjectName("pushButton")
        
        self.Pause = QPushButton(self.centralwidget)
        self.Pause.setGeometry(QRect(520, 370, 81, 81))
        self.Pause.setDefault(True)
        self.Pause.setFlat(False)
        self.Pause.setObjectName("pushButton_2")
        
        self.Stop = QPushButton(self.centralwidget)
        self.Stop.setGeometry(QRect(190, 370, 81, 81))
        self.Stop.setDefault(True)
        self.Stop.setObjectName("pushButton_3")
        
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setGeometry(QRect(0, 0, 784, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "Таймер"))
        self.Melody.setText(_translate("MainWindow", "Рингтон"))
        self.Pause.setText(_translate("MainWindow", "Пауза"))
        self.Stop.setText(_translate("MainWindow", "Отмена"))