1
我想做一個簡單的計算器,將需要2個數字,用戶輸入並將它們相乘。但是,當我將ans_string設置爲float時,代碼無法正常工作,但是如果將其設置爲str,它將起作用。PyQt不會顯示我的答案作爲一個浮點數
我希望能夠輸入帶小數點的數字,並且只在答案上顯示2個小數位。
如果我運行代碼ans_str我得到以下錯誤「AtrributeError:‘QLabel’對象有沒有屬性‘SETT’
from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication
from ui_multform import Ui_CalcWindow
class MainWindow(QMainWindow, Ui_CalcWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_CalcWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.mult)
def mult(self, value):
in_num1 = float(self.ui.lineEdit.text())
in_num2 = float(self.ui.lineEdit_2.text())
ans = in_num1 * in_num2
ans_string = float(ans) # << here ans_string is created
print (ans_string) # << Where u need to put it
self.ui.label_2.setText(ans_string)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv) # A new instance of QApplication
calculator = MainWindow() # We set the form to be our MainWindow (design)
calculator.show() # Show the form
sys.exit(app.exec_())