0% found this document useful (0 votes)
30 views

Source Code: No. Kalkulator - Py

This Python code defines a simple calculator application using PyQt5. It creates a main window with a grid layout containing number buttons, operator buttons, and an equals button. Functions are defined to update the display text when each button is clicked by appending the button value. An eval function calculates the result when equals is clicked and updates the display.

Uploaded by

Brayen Novendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Source Code: No. Kalkulator - Py

This Python code defines a simple calculator application using PyQt5. It creates a main window with a grid layout containing number buttons, operator buttons, and an equals button. Functions are defined to update the display text when each button is clicked by appending the button value. An eval function calculates the result when equals is clicked and updates the display.

Uploaded by

Brayen Novendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Source Code

No. Kalkulator.py

1 # Kalkulator Sederhana
2 from PyQt5.QtWidgets import *
3
4 app = QApplication([])
5
6 window = QMainWindow()
7 window.setGeometry(300,100,500,300)
8 window.setWindowTitle("Kalkulator")
9
10 grid = QGridLayout()
11
12 ednum = QLineEdit(window)
13 ednum.setFixedHeight(50)
14 ednum.setEnabled(False)
15
16 def buat_tombol(label):
17 qbtn = QPushButton(label, window)
18 qbtn.setFixedHeight(100)
19 return qbtn
20
21 def set0():
22 ednum.setText(f"{ednum.text()}0")
23 def set1():
24 ednum.setText(f"{ednum.text()}1")
25 def set2():
26 ednum.setText(f"{ednum.text()}2")
27 def set3():
28 ednum.setText(f"{ednum.text()}3")
29 def set4():
30 ednum.setText(f"{ednum.text()}4")
31 def set5():
32 ednum.setText(f"{ednum.text()}5")
33 def set6():
34 ednum.setText(f"{ednum.text()}6")
35 def set7():
36 ednum.setText(f"{ednum.text()}7")
37 def set8():
38 ednum.setText(f"{ednum.text()}8")
39 def set9():
40 ednum.setText(f"{ednum.text()}9")
41 def setplus():
42 ednum.setText(f"{ednum.text()}+")
43 def setmin():
44 ednum.setText(f"{ednum.text()}-")
45 def setkali():
46 ednum.setText(f"{ednum.text()}*")
47 def setbagi():
48 ednum.setText(f"{ednum.text()}/")
49 def clear():
50 ednum.setText("")
51 def sethasil():
52 x = eval(ednum.text())
53 ednum.setText(f"{x}")
54
55 deflist = [set0, set1, set2, set3, set4, set5, set6, set7, set8,
set9, setplus, setmin, setkali, setbagi]

56
btn: QPushButton = []
57
58
for i in range(10):
59
qbtn = buat_tombol(str(i))
60 qbtn.clicked.connect(deflist[i])
61 btn.append(qbtn)
62
63 btn_clear = buat_tombol("C")
64 btn_clear.clicked.connect(clear)
65
66 btn_kurang = buat_tombol("-")
67 btn_kurang.clicked.connect(setmin)
68 btn_tambah = buat_tombol("+")
69 btn_tambah.clicked.connect(setplus)
70
71 btn_kali = buat_tombol("*")
72 btn_kali.clicked.connect(setkali)
73 btn_bagi = buat_tombol("/")
74 btn_bagi.clicked.connect(setbagi)
75
76 btn_hasil = buat_tombol("=")
77 btn_hasil.clicked.connect(sethasil)
78
79 grid.addWidget(ednum, 0,0,1,4)
80 grid.addWidget(btn_clear, 1, 3)
81 grid.addWidget(btn_kurang, 2, 3)
82 grid.addWidget(btn_tambah, 3, 3)
83 grid.addWidget(btn_kali, 4, 0)
84 grid.addWidget(btn_bagi, 4, 2)
85 grid.addWidget(btn_hasil, 4, 3)
86
87 grid.addWidget(btn[7], 1, 0)
88 grid.addWidget(btn[8], 1, 1)
89 grid.addWidget(btn[9], 1, 2)
90
91 grid.addWidget(btn[4], 2, 0)
92 grid.addWidget(btn[5], 2, 1)
93 grid.addWidget(btn[6], 2, 2)
94
95 grid.addWidget(btn[1], 3, 0)
96 grid.addWidget(btn[2], 3, 1)
97 grid.addWidget(btn[3], 3, 2)
98
99 grid.addWidget(btn[0], 4, 1)
100
101 widget = QWidget()
102 widget.setLayout(grid)
103 window.setCentralWidget(widget)
104
105 window.show()
106 app.exec_()
107

You might also like