22
33import sys
44from functools import partial
5- from typing import NamedTuple
65
76from PyQt6 .QtCore import Qt
87from PyQt6 .QtWidgets import (
2120BUTTON_SIZE = 40
2221
2322
24- class GridPosition (NamedTuple ):
25- row : int
26- col : int
27-
28-
2923class PyCalcWindow (QMainWindow ):
3024 """PyCalc's main window (GUI or view)."""
3125
@@ -34,9 +28,9 @@ def __init__(self):
3428 self .setWindowTitle ("PyCalc" )
3529 self .setFixedSize (WINDOW_SIZE , WINDOW_SIZE )
3630 self .generalLayout = QVBoxLayout ()
37- self . _centralWidget = QWidget (self )
38- self . setCentralWidget (self ._centralWidget )
39- self ._centralWidget . setLayout ( self . generalLayout )
31+ centralWidget = QWidget (self )
32+ centralWidget . setLayout (self .generalLayout )
33+ self .setCentralWidget ( centralWidget )
4034 self ._createDisplay ()
4135 self ._createButtons ()
4236
@@ -50,36 +44,18 @@ def _createDisplay(self):
5044 def _createButtons (self ):
5145 self .buttonMap = {}
5246 buttonsLayout = QGridLayout ()
53- # Key Symbol | Key position
54- keyMap = {
55- "7" : GridPosition (0 , 0 ),
56- "8" : GridPosition (0 , 1 ),
57- "9" : GridPosition (0 , 2 ),
58- "/" : GridPosition (0 , 3 ),
59- "C" : GridPosition (0 , 4 ),
60- "4" : GridPosition (1 , 0 ),
61- "5" : GridPosition (1 , 1 ),
62- "6" : GridPosition (1 , 2 ),
63- "*" : GridPosition (1 , 3 ),
64- "(" : GridPosition (1 , 4 ),
65- "1" : GridPosition (2 , 0 ),
66- "2" : GridPosition (2 , 1 ),
67- "3" : GridPosition (2 , 2 ),
68- "-" : GridPosition (2 , 3 ),
69- ")" : GridPosition (2 , 4 ),
70- "0" : GridPosition (3 , 0 ),
71- "00" : GridPosition (3 , 1 ),
72- "." : GridPosition (3 , 2 ),
73- "+" : GridPosition (3 , 3 ),
74- "=" : GridPosition (3 , 4 ),
75- }
76-
77- for keySymbol , keyPosition in keyMap .items ():
78- self .buttonMap [keySymbol ] = QPushButton (keySymbol )
79- self .buttonMap [keySymbol ].setFixedSize (BUTTON_SIZE , BUTTON_SIZE )
80- buttonsLayout .addWidget (
81- self .buttonMap [keySymbol ], keyPosition .row , keyPosition .col
82- )
47+ keyBoard = [
48+ ["7" , "8" , "9" , "/" , "C" ],
49+ ["4" , "5" , "6" , "*" , "(" ],
50+ ["1" , "2" , "3" , "-" , ")" ],
51+ ["0" , "00" , "." , "+" , "=" ],
52+ ]
53+
54+ for row , keys in enumerate (keyBoard ):
55+ for col , key in enumerate (keys ):
56+ self .buttonMap [key ] = QPushButton (key )
57+ self .buttonMap [key ].setFixedSize (BUTTON_SIZE , BUTTON_SIZE )
58+ buttonsLayout .addWidget (self .buttonMap [key ], row , col )
8359
8460 self .generalLayout .addLayout (buttonsLayout )
8561
@@ -118,10 +94,10 @@ def _calculateResult(self):
11894 result = self ._evaluate (expression = self ._view .displayText ())
11995 self ._view .setDisplayText (result )
12096
121- def _buildExpression (self , sub_exp ):
97+ def _buildExpression (self , subExpression ):
12298 if self ._view .displayText () == ERROR_MSG :
12399 self ._view .clearDisplay ()
124- expression = self ._view .displayText () + sub_exp
100+ expression = self ._view .displayText () + subExpression
125101 self ._view .setDisplayText (expression )
126102
127103 def _connectSignalsAndSlots (self ):
0 commit comments