Last active
February 10, 2019 04:42
-
-
Save Kf4btg/02da3be56c46a24756ff14a4bc1cdb29 to your computer and use it in GitHub Desktop.
Decent starting point for pretty much any simple PyQt5 application
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
#!/usr/bin/env python3 | |
from PyQt5 import QtWidgets | |
from PyQt5.QtWidgets import QAction | |
from PyQt5.QtGui import QIcon, QKeySequence | |
class MainWindow(QtWidgets.QMainWindow): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.tableview = QtWidgets.QTableWidget(self) | |
self.setCentralWidget(self.tableview) | |
self._create_actions() | |
self._create_menus() | |
self._create_toolbars() | |
def closeEvent(self, event): | |
""" | |
Override closeEvent to check for unsaved changes and | |
to write application settings, if any. | |
""" | |
if self.check_modified(): | |
# self.write_settings() | |
event.accept() | |
else: | |
event.ignore() | |
def new(self): | |
print("new()") | |
def open(self): | |
print("open()") | |
def save(self): | |
print("save()") | |
def saveas(self): | |
print("saveas()") | |
def check_modified(self): | |
""" | |
If the environment has unsaved modifications, ask the user if | |
they would like to save their changes before continuing. | |
""" | |
## possible code: | |
# if self.is_modified: | |
# reply = QMessageBox.warning(self, "MyApplication", | |
# "Do you want to save your changes?", | |
# QMessageBox.Cancel|QMessageBox.Discard|QMessageBox.Save) | |
# | |
# if reply == QMessageBox.Save: | |
# return self.save() | |
# if reply == QMessageBox.Cancel: | |
# return False | |
# return True | |
return True | |
def _create_actions(self): | |
qks=QKeySequence | |
icon=QIcon().fromTheme | |
## main actions | |
self.action_new = QAction(icon("document-new"), | |
"&New", self, | |
shortcut=qks.New, | |
triggered=self.new) | |
self.action_open = QAction(icon("document-open"), | |
"&Open...", self, | |
shortcut=qks.Open, | |
triggered=self.open) | |
self.action_save = QAction(icon("document-save"), | |
"&Save", self, | |
shortcut=qks.Save, | |
triggered=self.save) | |
self.action_saveas = QAction(icon("document-save-as"), | |
"Save &As...", self, | |
shortcut=qks.SaveAs, | |
triggered=self.saveas) | |
self.action_quit = QAction(icon("application-exit"), | |
"&Quit", self, | |
shortcut=qks.Quit, | |
triggered=self.close) | |
## edit actions | |
# these will usually be connected to methods of the editor | |
# (e.g. the table widget, a text edit, etc.) | |
self.action_copy = QAction(icon("edit-copy"), | |
"&Copy", self, | |
shortcut=qks.Copy, | |
triggered=dummy) | |
self.action_cut = QAction(icon("edit-cut"), | |
"Cu&t", self, | |
shortcut=qks.Cut, | |
triggered=dummy) | |
self.action_paste = QAction(icon("edit-paste"), | |
"&Paste", self, | |
shortcut=qks.Paste, | |
triggered=dummy) | |
## disable some actions at application start; | |
## i.e. the ones that don't make sense without an open document | |
for a in (self.action_save, self.action_saveas, | |
self.action_copy, self.action_cut, self.action_paste): | |
a.setEnabled(False) | |
def _create_menus(self): | |
self.menu_file : QtWidgets.QMenu = self.menuBar().addMenu("&File") | |
self.menu_edit : QtWidgets.QMenu = self.menuBar().addMenu("&Edit") | |
self.menu_file.addAction(self.action_new) | |
self.menu_file.addAction(self.action_open) | |
self.menu_file.addAction(self.action_save) | |
self.menu_file.addAction(self.action_saveas) | |
self.menu_file.addSeparator() | |
self.menu_file.addAction(self.action_quit) | |
self.menu_edit.addAction(self.action_copy) | |
self.menu_edit.addAction(self.action_cut) | |
self.menu_edit.addAction(self.action_paste) | |
def _create_toolbars(self): | |
self.toolbar_file : QtWidgets.QToolBar = self.addToolBar("File") | |
self.toolbar_edit : QtWidgets.QToolBar = self.addToolBar("Edit") | |
self.toolbar_file.addAction(self.action_new) | |
self.toolbar_file.addAction(self.action_open) | |
self.toolbar_file.addAction(self.action_save) | |
self.toolbar_edit.addAction(self.action_copy) | |
self.toolbar_edit.addAction(self.action_cut) | |
self.toolbar_edit.addAction(self.action_paste) | |
def dummy(): | |
print("triggered") | |
def main(): | |
import sys | |
from PyQt5.QtWidgets import QApplication | |
app=QApplication(sys.argv) | |
mw=MainWindow() | |
mw.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was writing this for a new small project when it occurred to me that, at this stage, the code is a pretty good place to start for any number of GUI applications that fall within the typical "one-main-window" paradigm. Depending on the complexity, this could be a base to be expanded upon or very nearly the finished project.
Uses a few python3.6-isms (the type hints, mostly for an IDE's benefit) which can be safely removed without affecting the program's functionality. The QTableWidget is an obvious placeholder, as is the "dummy()" method, all of which should be replaced as needed.