|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +############################################################### |
| 4 | +# kenwaldek GPL-license |
| 5 | + |
| 6 | +# Title: PyQt5 lesson 15 Version: 1.0 |
| 7 | +# Date: 09-01-17 Language: python3 |
| 8 | +# Description: pyqt5 gui and file saving |
| 9 | +# pythonprogramming.net from PyQt4 to PyQt5 |
| 10 | +############################################################### |
| 11 | +# do something |
| 12 | + |
| 13 | + |
| 14 | +import sys |
| 15 | +from PyQt5.QtCore import QCoreApplication, Qt |
| 16 | +from PyQt5.QtGui import QIcon, QColor |
| 17 | +from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QAction, QMessageBox |
| 18 | +from PyQt5.QtWidgets import QCalendarWidget, QFontDialog, QColorDialog, QTextEdit, QFileDialog |
| 19 | +from PyQt5.QtWidgets import QCheckBox, QProgressBar, QComboBox, QLabel, QStyleFactory, QLineEdit, QInputDialog |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +class window(QMainWindow): |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + super(window, self).__init__() |
| 27 | + self.setGeometry(50, 50, 800, 500) |
| 28 | + self.setWindowTitle('pyqt5 Tut') |
| 29 | + self.setWindowIcon(QIcon('pic.png')) |
| 30 | + |
| 31 | + extractAction = QAction('&Get to the choppah', self) |
| 32 | + extractAction.setShortcut('Ctrl+Q') |
| 33 | + extractAction.setStatusTip('leave the app') |
| 34 | + extractAction.triggered.connect(self.close_application) |
| 35 | + |
| 36 | + openEditor = QAction('&Editor', self) |
| 37 | + openEditor.setShortcut('Ctrl+E') |
| 38 | + openEditor.setStatusTip('Open Editor') |
| 39 | + openEditor.triggered.connect(self.editor) |
| 40 | + |
| 41 | + openFile = QAction('&Open File', self) |
| 42 | + openFile.setShortcut('Ctrl+O') |
| 43 | + openFile.setStatusTip('Open File') |
| 44 | + openFile.triggered.connect(self.file_open) |
| 45 | + |
| 46 | + saveFile = QAction('&Save File', self) |
| 47 | + saveFile.setShortcut('Ctrl+S') |
| 48 | + saveFile.setStatusTip('Save File') |
| 49 | + saveFile.triggered.connect(self.file_save) |
| 50 | + |
| 51 | + |
| 52 | + self.statusBar() |
| 53 | + |
| 54 | + mainMenu = self.menuBar() |
| 55 | + fileMenu = mainMenu.addMenu('&File') |
| 56 | + fileMenu.addAction(extractAction) |
| 57 | + |
| 58 | + fileMenu.addAction(openFile) |
| 59 | + fileMenu.addAction(saveFile) |
| 60 | + |
| 61 | + |
| 62 | + editorMenu = mainMenu.addMenu('&Editor') |
| 63 | + editorMenu.addAction(openEditor) |
| 64 | + |
| 65 | + extractAction = QAction(QIcon('pic.png'), 'flee the scene', self) |
| 66 | + extractAction.triggered.connect(self.close_application) |
| 67 | + self.toolBar = self.addToolBar('extraction') |
| 68 | + self.toolBar.addAction(extractAction) |
| 69 | + |
| 70 | + fontChoice = QAction('Font', self) |
| 71 | + fontChoice.triggered.connect(self.font_choice) |
| 72 | + # self.toolBar = self.addToolBar('Font') |
| 73 | + self.toolBar.addAction(fontChoice) |
| 74 | + |
| 75 | + cal = QCalendarWidget(self) |
| 76 | + cal.move(500, 200) |
| 77 | + cal.resize(200, 200) |
| 78 | + |
| 79 | + self.home() |
| 80 | + |
| 81 | + def editor(self): |
| 82 | + self.textEdit = QTextEdit() |
| 83 | + self.setCentralWidget(self.textEdit) |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + def file_open(self): |
| 88 | + # need to make name an tupple otherwise i had an error and app crashed |
| 89 | + name, _ = QFileDialog.getOpenFileName(self, 'Open File', options=QFileDialog.DontUseNativeDialog) |
| 90 | + print('tot na dialog gelukt') # for debugging |
| 91 | + file = open(name, 'r') |
| 92 | + print('na het inlezen gelukt') # for debugging |
| 93 | + self.editor() |
| 94 | + |
| 95 | + with file: |
| 96 | + text = file.read() |
| 97 | + self.textEdit.setText(text) |
| 98 | + |
| 99 | + def file_save(self): |
| 100 | + name, _ = QFileDialog.getSaveFileName(self,'Save File') |
| 101 | + file = open(name, 'w') |
| 102 | + text = self.textEdit.toPlainText() |
| 103 | + file.write(text) |
| 104 | + file.close() |
| 105 | + |
| 106 | + |
| 107 | +##End of code problem |
| 108 | + |
| 109 | + def color_picker(self): |
| 110 | + color = QColorDialog.getColor() |
| 111 | + self.styleChoice.setStyleSheet('QWidget{background-color: %s}' % color.name()) |
| 112 | + |
| 113 | + def font_choice(self): |
| 114 | + font, valid = QFontDialog.getFont() |
| 115 | + if valid: |
| 116 | + self.styleChoice.setFont(font) |
| 117 | + |
| 118 | + def home(self): |
| 119 | + btn = QPushButton('quit', self) |
| 120 | + btn.clicked.connect(self.close_application) |
| 121 | + btn.resize(btn.sizeHint()) |
| 122 | + btn.move(0, 100) |
| 123 | + |
| 124 | + checkBox = QCheckBox('Enlarge window', self) |
| 125 | + # checkBox.toggle() # if you want to be checked in in the begin |
| 126 | + checkBox.move(0, 50) |
| 127 | + checkBox.stateChanged.connect(self.enlarge_window) |
| 128 | + |
| 129 | + self.progress = QProgressBar(self) |
| 130 | + self.progress.setGeometry(200, 80, 250, 20) |
| 131 | + |
| 132 | + self.btn = QPushButton('download', self) |
| 133 | + self.btn.move(200, 120) |
| 134 | + self.btn.clicked.connect(self.download) |
| 135 | + |
| 136 | + self.styleChoice = QLabel('Windows', self) |
| 137 | + comboBox = QComboBox(self) |
| 138 | + comboBox.addItem('motif') |
| 139 | + comboBox.addItem('Windows') |
| 140 | + comboBox.addItem('cde') |
| 141 | + comboBox.addItem('Plastique') |
| 142 | + comboBox.addItem('Cleanlooks') |
| 143 | + comboBox.addItem('windowsvista') |
| 144 | + |
| 145 | + comboBox.move(25, 250) |
| 146 | + self.styleChoice.move(25, 150) |
| 147 | + comboBox.activated[str].connect(self.style_choice) |
| 148 | + |
| 149 | + color = QColor(0,0,0) |
| 150 | + fontColer = QAction('font bg color', self) |
| 151 | + fontColer.triggered.connect(self.color_picker) |
| 152 | + self.toolBar.addAction(fontColer) |
| 153 | + |
| 154 | + self.show() |
| 155 | + |
| 156 | + def style_choice(self, text): |
| 157 | + self.styleChoice.setText(text) |
| 158 | + QApplication.setStyle(QStyleFactory.create(text)) |
| 159 | + |
| 160 | + def download(self): |
| 161 | + self.completed = 0 |
| 162 | + |
| 163 | + while self.completed < 100: |
| 164 | + self.completed += 0.0001 |
| 165 | + self.progress.setValue(self.completed) |
| 166 | + |
| 167 | + |
| 168 | + def enlarge_window(self, state): |
| 169 | + if state == Qt.Checked: |
| 170 | + self.setGeometry(50, 50, 1000, 600) |
| 171 | + else: |
| 172 | + self.setGeometry(50, 50 , 500, 300) |
| 173 | + |
| 174 | + |
| 175 | + def close_application(self): |
| 176 | + |
| 177 | + choice = QMessageBox.question(self, 'Message', |
| 178 | + "Are you sure to quit?", QMessageBox.Yes | |
| 179 | + QMessageBox.No, QMessageBox.No) |
| 180 | + |
| 181 | + if choice == QMessageBox.Yes: |
| 182 | + print('quit application') |
| 183 | + sys.exit() |
| 184 | + else: |
| 185 | + pass |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == "__main__": # had to add this otherwise app crashed |
| 189 | + |
| 190 | + def run(): |
| 191 | + app = QApplication(sys.argv) |
| 192 | + Gui = window() |
| 193 | + sys.exit(app.exec_()) |
| 194 | + |
| 195 | +run() |
0 commit comments