-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
108 lines (75 loc) · 3.02 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
import os
import sys
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from calc import Calculator
class Window(QWidget):
liste = ["+","-","/","*","1","2","3","AC","4","5","6","0","7","8","9","="]
extraOperator = ["+/-","=","AC"]
current_dir = os.path.dirname(os.path.realpath(__file__))
def __init__(self):
super(QWidget,self).__init__()
self.setupUI()
def setupUI(self):
self.setWindowTitle("Taschenrechner")
self.resize(280,300)
gridLayout = QGridLayout()
gridLayout.setGeometry(QRect(20,80,240,220))
gridLayout.setObjectName("gridBase")
gridLayout.setContentsMargins(5,5,5,5)
self.buttons = []
c = 0; b = 0
for _ in list(range(0,(4*4))):
self.buttons.append(QPushButton(self.liste[_]))
self.buttons[_].setObjectName("pushbutton" + str(_))
self.buttons[_].clicked.connect(self.triggerClick)
if c >= 4: c = 0; b += 1
self.buttons[_].setText(self.liste[_])
gridLayout.addWidget(self.buttons[_],b + 1,c,1,1)
c += 1
self.textLine = QLineEdit("")
self.textLine.setAttribute(Qt.WidgetAttribute.WA_MacShowFocusRect,0)
self.textLine.textEdited.connect(self.onlyDefined)
gridLayout.addWidget(self.textLine,0,0,1,4)
self.setLayout(gridLayout)
filename = os.path.join(self.current_dir, "style/calc.css")
self.setStyleSheet(open(filename,"r").read())
self.show()
def triggerClick(self):
sender = self.sender()
if sender.text() in self.liste and not (self.textLine.text()[-1:] in ["+","-","*","/"] and sender.text() in ["+","-","*","/"]):
if sender.text() in self.extraOperator:
self.clearAC(sender.text())
self.calc(sender.text())
else:
self.textLine.setText(self.textLine.text() + sender.text())
def onlyDefined(self, e):
if e[-1:] in self.liste and not (e[-2:-1] in ["+","-","*","/"] and e[-1:] in ["+","-","*","/"]):
if e[-1:] in self.extraOperator:
self.clearAC(e[-1:])
self.calc(e[-1:])
else:
self.textLine.setText(e)
else:
self.textLine.setText(e[:-1])
def clearAC(self,i):
if i == 'AC':
self.textLine.setText("")
def calc(self,i):
self.setWindowTitle("Calculator")
if i == "=":
formel = self.textLine.text().replace("=","")
clc = Calculator()
try:
r = clc.worker(formel)
except ZeroDivisionError as ident:
r = ""
self.setWindowTitle("Zero Divison Error")
self.textLine.setText(str(r))
def keyPressEvent(self,e):
if e.key() == Qt.Key.Key_Return or e.key() == Qt.Key.Key_Enter:
self.calc("=")
app = QApplication(sys.argv)
w = Window()
sys.exit(app.exec())