Answer the question
In order to leave comments, you need to log in
Error with pyqt 5?
Recently I just started working with pyqt5, I decided to make a calculator for myself, for the sake of practice, everything worked out, it seems like, but always when I click "Equal" my calculator crashes, with many code variations, I don’t know if I’m looking for a problem there. Please tell me the errors, thanks)
import PyQt5.QtWidgets as qwt
class MainWindow(qwt.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Calculator")
self.setLayout(qwt.QVBoxLayout())
self.keypad()
self.temp_nums = []
self.show()
def keypad(self):
container = qwt.QWidget()
container.setLayout(qwt.QGridLayout())
self.result = qwt.QLineEdit()
btn_ravno = qwt.QPushButton("Равно", clicked = self.func_result)
btn_clear = qwt.QPushButton("Стереть", clicked = self.clear_calc)
btn1 = qwt.QPushButton("1", clicked = lambda: self.num_press("1"))
btn2 = qwt.QPushButton("2", clicked = lambda: self.num_press("2"))
btn3 = qwt.QPushButton("3", clicked = lambda: self.num_press("3"))
btn4 = qwt.QPushButton("4", clicked = lambda: self.num_press("4"))
btn5 = qwt.QPushButton("5", clicked = lambda: self.num_press("5"))
btn6 = qwt.QPushButton("6", clicked = lambda: self.num_press("6"))
btn7 = qwt.QPushButton("7", clicked = lambda: self.num_press("7"))
btn8 = qwt.QPushButton("8", clicked = lambda: self.num_press("8"))
btn9 = qwt.QPushButton("9", clicked = lambda: self.num_press("9"))
btn0 = qwt.QPushButton("0", clicked = lambda: self.num_press("0"))
btn_plus = qwt.QPushButton("+", clicked = lambda: self.num_press("+"))
btn_minus = qwt.QPushButton("-", clicked = lambda: self.num_press("-"))
btn_delit = qwt.QPushButton("/", clicked = lambda: self.num_press("/"))
btn_umn = qwt.QPushButton("*", clicked = lambda: self.num_press("*"))
container.layout().addWidget(self.result, 0, 0, 1, 4)
container.layout().addWidget(btn_ravno, 1, 0, 1, 2)
container.layout().addWidget(btn_clear, 1, 2, 1, 2)
container.layout().addWidget(btn9, 2, 0)
container.layout().addWidget(btn8, 2, 1)
container.layout().addWidget(btn7, 2, 2)
container.layout().addWidget(btn_plus, 2, 3)
container.layout().addWidget(btn1, 4, 2)
container.layout().addWidget(btn2, 4, 1)
container.layout().addWidget(btn3, 4, 0)
container.layout().addWidget(btn4, 3, 2)
container.layout().addWidget(btn5, 3, 1)
container.layout().addWidget(btn6, 3, 0)
container.layout().addWidget(btn0, 5, 0, 1, 3)
container.layout().addWidget(btn_delit, 5, 3)
container.layout().addWidget(btn_minus, 3, 3)
container.layout().addWidget(btn_umn, 4, 3)
self.layout().addWidget(container)
def num_press(self, key_number):
global na_tablo
self.temp_nums.append(key_number)
na_tablo = "".join(self.temp_nums)
self.result.setText(na_tablo)
def func_result(self):
self.result.clear()
self.result.setText(eval(na_tablo))
def clear_calc(self):
self.result.clear()
self.temp_nums = []
app = qwt.QApplication([])
mw = MainWindow()
app.setStyle(qwt.QStyleFactory.create("Fusion"))
app.exec_()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question