Selamın Aleyküm.
Konu Gayet Net Bence
Konu Gayet Net Bence
Python:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLineEdit, QPushButton, QStyleFactory
from PyQt5.QtCore import Qt
class CalculatorApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('TurkHackTeam.org')
self.setGeometry(100, 100, 300, 400)
self.layout = QGridLayout()
self.result_display = QLineEdit(self)
self.result_display.setReadOnly(True)
self.result_display.setAlignment(Qt.AlignRight)
self.result_display.setProperty("class", "display")
self.layout.addWidget(self.result_display, 0, 0, 1, 4)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'C', '<-',
]
button_grid = [
(i, j) for i in range(1, 7) for j in range(4)
]
for (i, j), button_text in zip(button_grid, buttons):
button = QPushButton(button_text, self)
button.clicked.connect(self.buttonClicked)
button.setProperty("class", "button")
self.layout.addWidget(button, i, j)
self.setLayout(self.layout)
self.current_expression = ''
def buttonClicked(self):
sender = self.sender()
clicked_text = sender.text()
if clicked_text == '=':
try:
result = eval(self.current_expression)
self.result_display.setText(str(result))
self.current_expression = str(result)
except Exception as e:
self.result_display.setText('Error')
self.current_expression = ''
elif clicked_text == 'C':
self.result_display.clear()
self.current_expression = ''
elif clicked_text == '<-':
self.current_expression = self.current_expression[:-1]
self.result_display.setText(self.current_expression)
else:
self.current_expression += clicked_text
self.result_display.setText(self.current_expression)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle(QStyleFactory.create("Windows"))
calc_app = CalculatorApp()
calc_app.show()
sys.exit(app.exec_())