Created
May 10, 2022 19:38
-
-
Save RayyanNafees/86f2290eb0a1a03930de776effb53a7a to your computer and use it in GitHub Desktop.
A fiddle example for PyQt4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| # coding: utf-8 | |
| from PyQt4.QtCore import * | |
| from PyQt4.QtGui import * | |
| import sys | |
| class MainWindow(QMainWindow): | |
| def __init__(self, parent=None): | |
| super(MainWindow, self).__init__(parent) | |
| self.filename = None | |
| self.dirty = False | |
| self.listWidget = QListWidget() | |
| self.setCentralWidget(self.listWidget) | |
| fileNewAction = self.createAction("&New...", self.fileNew, | |
| QKeySequence.New, "filenew") | |
| fileOpenAction = self.createAction("&Open...", self.fileOpen, | |
| QKeySequence.Open, "fileopen") | |
| fileSaveAction = self.createAction("&Save", self.fileSave, | |
| QKeySequence.Save, "filesave") | |
| fileMenu = self.menuBar().addMenu("&File") | |
| fileMenu.addAction(fileNewAction) | |
| fileMenu.addAction(fileOpenAction) | |
| fileMenu.addAction(fileSaveAction) | |
| fileToolbar = self.addToolBar("File") | |
| fileToolbar.addAction(fileNewAction) | |
| fileToolbar.addAction(fileOpenAction) | |
| fileToolbar.addAction(fileSaveAction) | |
| self.statusBar().showMessage("Ready...", 5000) | |
| self.setWindowTitle("ListKeeper - Unnamed List") | |
| def editAdd(self): | |
| form = AddForm(self) | |
| if form.exec_(): | |
| if form.addAtEnd: | |
| self.listWidget.addItem(form.text) | |
| else: | |
| self.listWidget.insertItem(0, form.text) | |
| self.dirty = True | |
| class AddForm(QDialog): | |
| def __init__(self, parent=None): | |
| super(AddForm, self).__init__(parent) | |
| self.textLineEdit = QLineEdit() | |
| label = QLabel("&Add Text:") | |
| label.setBuddy(self.textLineEdit) | |
| self.addAtStartRadioButton = QRadioButton("Add at &Start") | |
| self.addAtEndRadioButton = QRadioButton("Add at &End") | |
| self.addAtEndRadioButton.setChecked(True) | |
| buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) | |
| topLayout = QHBoxLayout() | |
| topLayout.addWidget(label) | |
| topLayout.addWidget(self.textLineEdit) | |
| middleLayout = QHBoxLayout() | |
| middleLayout.addWidget(self.addAtStartRadioButton) | |
| middleLayout.addWidget(self.addAtEndRadioButton) | |
| layout = QVBoxLayout() | |
| layout.addLayout(topLayout) | |
| layout.addLayout(middleLayout) | |
| layout.addStretch() | |
| layout.addWidget(buttonBox) | |
| self.setLayout(layout) | |
| buttonBox.accepted.connect(self.accept) | |
| buttonBox.rejected.connect(self.reject) | |
| self.setWindowTitle("ListKeeper - Add") | |
| def accept(self): | |
| self.text = self.textLineEdit.text() | |
| if self.text.isEmpty(): | |
| super(AddForm, self).reject() | |
| else: | |
| self.addAtEnd = self.addAtEndRadioButton.isChecked() | |
| super(AddForm, self).accept() | |
| class Squiggly(QWidget): | |
| textChanged = pyqtSignal(str) | |
| def __init__(self, parent=None): | |
| super(Squiggly, self).__init__(parent) | |
| font = self.font() | |
| font.setPointSize(font.pointSize() + 80) | |
| self.setFont(font) | |
| mstring = 'I rule' | |
| self.text = QString(mstring) | |
| self.step = 0; | |
| self.setWindowTitle(mstring) | |
| self.resize(500, 360) | |
| self.timer = QBasicTimer() | |
| self.timer.start(20, self) | |
| def timerEvent(self, event): | |
| if (event.timerId() == self.timer.timerId()): | |
| self.step += 1 | |
| self.update() | |
| else: | |
| super(Squiggly, self).timerEvent(event) | |
| def paintEvent(self, event): | |
| sines = (0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38) | |
| fm = QFontMetrics(self.font()) | |
| x = (self.width() - fm.width(self.text)) / 2 | |
| y = (self.height() + fm.ascent() - fm.descent()) / 2 | |
| color = QColor() | |
| painter = QPainter(self) | |
| for i in range(self.text.size()): | |
| index = (self.step + i) % 16 | |
| color.setHsv((15 - index) * 16, 255, 191) | |
| painter.setPen(color) | |
| painter.drawText(x, y - ((sines[index] * fm.height()) / 400), self.text[i]) | |
| x += fm.width(self.text[i]) | |
| def keyPressEvent(self, event): | |
| if event.key() in (Qt.Key_Q, Qt.Key_X, Qt.Key_Escape): | |
| self.close() | |
| else: | |
| super(Squiggly, self).keyPressEvent(event) | |
| def mousePressEvent(self, event): | |
| text, ok = QInputDialog.getText(self, "Squiggly - Set Text", "Text:", | |
| QLineEdit.Normal, self.text) | |
| if ok and not text.isEmpty(): | |
| self.text = text | |
| self.update() | |
| self.textChanged.emit(self.text) | |
| if __name__ == "__main__": | |
| app = QApplication(sys.argv) | |
| squiggly = Squiggly() | |
| squiggly.show(); | |
| app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment