This repository was archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
184 lines (151 loc) · 6.29 KB
/
main.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#
# MIT License
#
# Copyright (c) 2023 Erriez
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Source: https://github.com/Erriez/pyside6-nuitka-deployment
#
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QFileDialog, QMessageBox
from PySide6.QtGui import QAction, QIcon
import PySide6
import argparse
import os
import sys
import webbrowser
APP_NAME = 'PySide6 Example'
APP_WEBSITE = 'https://github.com/Erriez/pyside6-nuitka-deployment'
APP_DEVELOPER = 'Erriez'
APP_YEAR = 2023
APP_LICENSE = 'MIT'
# When packaged to a single file with PyInstaller, running the .exe will unpack
# everything to a folder in your TEMP directory, run the script, then discard
# the temporary files. The path of the temporary folder changes with each
# running, but a reference to its location is added to sys as sys._MEIPASS.
try:
os.chdir(sys._MEIPASS)
print('Using ' + sys._MEIPASS)
except:
pass
# Find images with Nuitka build option "--include-data-dir=src=dst"
def resource_path(filename):
# Get the absolute path of the directory containing the executable
exe_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the path to the dst file
dst_path = os.path.join(exe_dir, filename)
# Return the image path
return dst_path
def get_app_version():
app_version = 'Unknown'
version_file = resource_path(r'data/version.txt')
if version_file:
try:
with open(version_file, 'r') as f:
app_version = f.readline()
except OSError:
pass
return app_version.strip()
class Window(QMainWindow):
def __init__(self):
super().__init__()
# Resize window
self.resize(400, 300)
# Set window title
self.setWindowTitle(APP_NAME)
# Set window icon
self.setWindowIcon(QIcon(resource_path(r'images/app.png')))
# Create textbox
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
# Create menubar
menubar = self.menuBar()
file_open_action = QAction(QIcon(resource_path(r'images/open.png')), 'Open', self)
file_open_action.setShortcut('Ctrl+O')
file_open_action.setStatusTip('Open new File')
file_open_action.triggered.connect(self.show_dialog)
file_exit_action = QAction(QIcon(resource_path(r'images/exit.png')), '&Exit', self)
file_exit_action.setShortcut('Ctrl+Q')
file_exit_action.setStatusTip('Exit application')
file_exit_action.triggered.connect(self.close)
menu_file = menubar.addMenu('&File')
menu_file.addAction(file_open_action)
menu_file.addSeparator()
menu_file.addAction(file_exit_action)
help_action = QAction(QIcon(resource_path(r'images/web.png')), '&Source on Github', self)
help_action.setShortcut('F1')
help_action.setStatusTip('Open Github project website')
help_action.triggered.connect(self.help)
about_action = QAction(QIcon(resource_path(r'images/question.png')), '&About', self)
about_action.setShortcut('Ctrl+?')
about_action.setStatusTip('About application')
about_action.triggered.connect(self.about)
menu_help = menubar.addMenu('&Help')
menu_help.addAction(help_action)
menu_help.addSeparator()
menu_help.addAction(about_action)
# Create toolbar with Exit button
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(file_open_action)
self.toolbar.addAction(file_exit_action)
self.toolbar.addSeparator()
self.toolbar.addAction(help_action)
# Create statusbar
self.statusBar().showMessage('Click File | Open to read a file')
def show_dialog(self):
# https://doc.qt.io/qtforpython/PySide6/QtWidgets/QFontDialog.html
path, _ = QFileDialog.getOpenFileName(self, 'Open file', '/home')
if not path:
self.statusBar().showMessage('No file selected'.format())
elif not os.path.exists(path):
self.statusBar().showMessage('File {} not found'.format(path))
else:
try:
with open(path, 'r') as f:
data = f.read()
self.textEdit.setText(data)
self.statusBar().showMessage('File {} opened'.format(path))
except OSError as err:
self.statusBar().showMessage(err)
def help(self):
webbrowser.open(APP_WEBSITE)
def about(self, _):
title = 'About'
text = f'Application: {APP_NAME}\n' \
f'Version: {get_app_version()}\n' \
f'Python: v{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}\n' \
f'PySide: v{PySide6.__version__}\n' \
f'Copyright: © {APP_YEAR} by {APP_DEVELOPER}\n' \
f'License: {APP_LICENSE}'
QMessageBox.about(self, title, text)
def main():
print(f'{APP_NAME}')
# Argument parser
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', help='Print version commandline', action="store_true")
args = parser.parse_args()
if args.version:
print(f'Version: {get_app_version()}')
else:
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()