1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sys
import pkg_resources
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QAction, QApplication, QDesktopWidget, QDialog, QFileDialog,
QHBoxLayout, QLabel, QMainWindow, QToolBar, QVBoxLayout, QWidget)
class BenApp(QMainWindow):
"""Create the main window that stores all of the widgets necessary for the application."""
def __init__(self, parent=None):
"""Initialize the components of the main window."""
super(BenApp, self).__init__(parent)
self.resize(1024, 768)
self.setWindowTitle('BenApp')
window_icon = pkg_resources.resource_filename('workshopapp.images',
'ic_insert_drive_file_black_48dp_1x.png')
self.setWindowIcon(QIcon(window_icon))
self.widget = QWidget()
self.layout = QHBoxLayout(self.widget)
self.menu_bar = self.menuBar()
self.about_dialog = AboutDialog()
self.status_bar = self.statusBar()
self.status_bar.showMessage('Ready', 5000)
self.file_menu()
self.help_menu()
self.tool_bar_items()
def file_menu(self):
"""Create a file submenu with an Open File item that opens a file dialog."""
self.file_sub_menu = self.menu_bar.addMenu('File')
self.open_action = QAction('Open File', self)
self.open_action.setStatusTip('Open a file into BenApp.')
self.open_action.setShortcut('CTRL+O')
self.open_action.triggered.connect(self.open_file)
self.exit_action = QAction('Exit Application', self)
self.exit_action.setStatusTip('Exit the application.')
self.exit_action.setShortcut('CTRL+Q')
self.exit_action.triggered.connect(lambda: QApplication.quit())
self.file_sub_menu.addAction(self.open_action)
self.file_sub_menu.addAction(self.exit_action)
def help_menu(self):
"""Create a help submenu with an About item tha opens an about dialog."""
self.help_sub_menu = self.menu_bar.addMenu('Help')
self.about_action = QAction('About', self)
self.about_action.setStatusTip('About the application.')
self.about_action.setShortcut('CTRL+H')
self.about_action.triggered.connect(lambda: self.about_dialog.exec_())
self.help_sub_menu.addAction(self.about_action)
def tool_bar_items(self):
"""Create a tool bar for the main window."""
self.tool_bar = QToolBar()
self.addToolBar(Qt.TopToolBarArea, self.tool_bar)
self.tool_bar.setMovable(False)
open_icon = pkg_resources.resource_filename('workshopapp.images',
'ic_open_in_new_black_48dp_1x.png')
tool_bar_open_action = QAction(QIcon(open_icon), 'Open File', self)
tool_bar_open_action.triggered.connect(self.open_file)
self.tool_bar.addAction(tool_bar_open_action)
def open_file(self):
"""Open a QFileDialog to allow the user to open a file into the application."""
filename, accepted = QFileDialog.getOpenFileName(self, 'Open File')
if accepted:
with open(filename) as file:
file.read()
class AboutDialog(QDialog):
"""Create the necessary elements to show helpful text in a dialog."""
def __init__(self, parent=None):
"""Display a dialog that shows application information."""
super(AboutDialog, self).__init__(parent)
self.setWindowTitle('About')
help_icon = pkg_resources.resource_filename('workshopapp.images',
'ic_help_black_48dp_1x.png')
self.setWindowIcon(QIcon(help_icon))
self.resize(300, 200)
author = QLabel('Benjama EEP')
author.setAlignment(Qt.AlignCenter)
icons = QLabel('Material design icons created by Google')
icons.setAlignment(Qt.AlignCenter)
github = QLabel('GitHub: benjama.eep')
github.setAlignment(Qt.AlignCenter)
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignVCenter)
self.layout.addWidget(author)
self.layout.addWidget(icons)
self.layout.addWidget(github)
self.setLayout(self.layout)
def main():
application = QApplication(sys.argv)
window = BenApp()
desktop = QDesktopWidget().availableGeometry()
width = (desktop.width() - window.width()) / 2
height = (desktop.height() - window.height()) / 2
window.show()
window.move(width, height)
sys.exit(application.exec_())