-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
145 lines (101 loc) · 3.66 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
#!/usr/bin/env python3
# Filename: pycalc.py
# 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.
"""PyCalc is a simple calculator built using Python.
The application is implemented using the MVC pattern. Views and controllers
are provided for PyQt5, PySide2, wxPython and Tkinter. Every time you run the
app, the view changes alternatively from a framework to another."""
import sys
from pycalc.evaluator import Evaluator
from pycalc.config import PyCalcConfig
__version__ = "1.0"
__author__ = "Leodanis Pozo Ramos"
# Client code
def main():
"""Main function."""
ui = PyCalcConfig('config.ini').read()
try:
globals()[f'{ui}_app']()
except ModuleNotFoundError as e:
print(e, file=sys.stderr)
def pyqt5_app():
"""PyQt5 implementation."""
PyCalcConfig('config.ini').write()
# Import QApplication from PyQt5.QtWidgets
from PyQt5.QtWidgets import QApplication
# Import the view and the controller
from pycalc.pyqt5_vc.qt_view import PyCalcUi
from pycalc.pyqt5_vc.qt_controller import PyCalcCtrl
# Create an instance of QApplication
pycalc = QApplication(sys.argv)
# Create the calculator's GUI
view = PyCalcUi()
# Create the model
model = Evaluator()
# Create the controller and run it
controller = PyCalcCtrl(model=model, view=view)
controller.run()
# Execute calculator's main loop
sys.exit(pycalc.exec_())
def pyside2_app():
"""PySide2 implementation."""
PyCalcConfig('config.ini').write()
# Import QApplication from PySide2.QtWidgets
from PySide2.QtWidgets import QApplication
# Import the view and the controller
from pycalc.pyside2_vc.ps2_view import PyCalcUi
from pycalc.pyside2_vc.ps2_controller import PyCalcCtrl
# Create an instance of QApplication
pycalc = QApplication(sys.argv)
# Create the calculator's GUI
view = PyCalcUi()
# Create the model
model = Evaluator()
# Create the controller and run it
controller = PyCalcCtrl(model=model, view=view)
controller.run()
# Execute calculator's main loop
sys.exit(pycalc.exec_())
def tkinter_app():
"""Tkinter implementation."""
PyCalcConfig('config.ini').write()
# Import the view and the controller
from pycalc.tkinter_vc.tk_view import PyCalcUi
from pycalc.tkinter_vc.tk_controller import PyCalcCtrl
# Create the calculator's GUI
view = PyCalcUi()
# Create the model
model = Evaluator()
# Create the controller and run it
controller = PyCalcCtrl(model, view)
controller.run()
# Execute calculator's main loop
view.mainloop()
def wxpython_app():
"""wxPython implementation."""
PyCalcConfig('config.ini').write()
# Import wx package
import wx
# Import the view and the controller
from pycalc.wxpython_vc.wx_view import PyCalcUi
from pycalc.wxpython_vc.wx_controller import PyCalcCtrl
# Create an instance of wx.App
pycalc = wx.App()
# Create the calculator's GUI
view = PyCalcUi()
pycalc.SetTopWindow(view)
# Create the model
model = Evaluator()
# Create the controller and run it
controller = PyCalcCtrl(model, view)
controller.run()
# Execute calculator's main loop
pycalc.MainLoop()
if __name__ == "__main__":
main()