import sys from PyQt5.QtWidgets import QApplication, QWidget , QMainWindow from Calui1 import Ui_Form class Mywindow(QWidget,Ui_Form): def init(self): super(Mywindow,self).init() self.setupUi(self) def btnClear_clicked(self): self.lineEdit.clear() def btn1(self): self.lineEdit.insert("1") def btn2(self): self.lineEdit.insert("2") def btn3(self): self.lineEdit.insert("3") def btn4(self): self.lineEdit.insert("4") def btn5(self): self.lineEdit.insert("5") def btn6(self): self.lineEdit.insert("6") def btn7(self): self.lineEdit.insert("7") def btn8(self): self.lineEdit.insert("8") def btn9(self): self.lineEdit.insert("9") def btn0(self): self.lineEdit.insert("0") def btn_plus(self): self.lineEdit.insert("+") def btn_sub(self): self.lineEdit.insert("-") def btn_mul(self): self.lineEdit.insert("*") def btn_div(self): self.lineEdit.insert("/") def btnequal(self): text=self.lineEdit.text() self.lineEdit.insert("= %.2f" %(eval(text))) if name=='main': app=QApplication(sys.argv) win=Mywindow() win.pushButton_7.clicked.connect(win.btnClear_clicked()) win.pushButton.clicked.connect(win.btn1()) win.pushButton_2.clicked.connect(win.btn2()) win.pushButton_3.clicked.connect(win.btn3()) win.pushButton_4.clicked.connect(win.btn_plus()) win.pushButton_5.clicked.connect(win.btn4()) win.pushButton_6.clicked.connect(win.btn7()) win.pushButton_8.clicked.connect(win.btn5()) win.pushButton_9.clicked.connect(win.btn8()) win.pushButton_10.clicked.connect(win.btn0()) win.pushButton_11.clicked.connect(win.btn6()) win.pushButton_12.clicked.connect(win.btn9()) win.pushButton_13.clicked.connect(win.btnequal()) win.pushButton_14.clicked.connect(win.btn_sub()) win.pushButton_15.clicked.connect(win.btn_mul()) win.pushButton_16.clicked.connect(win.btn_div()) win.show() sys.exit(app.exec_())为什么会报错
时间: 2024-02-06 17:09:15 浏览: 172
PyQt5 tutorial.rar_pyqt_pyqt5_python qt_python pyqt_python pyqt
这段代码有几个问题:
1. 在第一行导入语句中,`from PyQt5.QtWidgets import QApplication, QWidget , QMainWindow` 中间缺少逗号,应该为 `from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow`。
2. 在 `Mywindow` 类的构造函数中,`init` 方法应该为 `__init__`。
3. 在 `Mywindow` 类中,所有的按钮槽函数应该以 `on_` 开头,例如 `on_btnClear_clicked`。
4. 在 `btnequal` 方法中,如果用户输入了非法表达式,可能会引发 `ZeroDivisionError` 异常,建议添加异常处理代码。
修改后的代码如下:
```
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from Calui1 import Ui_Form
class Mywindow(QWidget,Ui_Form):
def __init__(self):
super(Mywindow,self).__init__()
self.setupUi(self)
def on_btnClear_clicked(self):
self.lineEdit.clear()
def on_btn1_clicked(self):
self.lineEdit.insert("1")
def on_btn2_clicked(self):
self.lineEdit.insert("2")
def on_btn3_clicked(self):
self.lineEdit.insert("3")
def on_btn4_clicked(self):
self.lineEdit.insert("4")
def on_btn5_clicked(self):
self.lineEdit.insert("5")
def on_btn6_clicked(self):
self.lineEdit.insert("6")
def on_btn7_clicked(self):
self.lineEdit.insert("7")
def on_btn8_clicked(self):
self.lineEdit.insert("8")
def on_btn9_clicked(self):
self.lineEdit.insert("9")
def on_btn0_clicked(self):
self.lineEdit.insert("0")
def on_btn_plus_clicked(self):
self.lineEdit.insert("+")
def on_btn_sub_clicked(self):
self.lineEdit.insert("-")
def on_btn_mul_clicked(self):
self.lineEdit.insert("*")
def on_btn_div_clicked(self):
self.lineEdit.insert("/")
def on_btnequal_clicked(self):
try:
text = self.lineEdit.text()
result = eval(text)
self.lineEdit.setText("= %.2f" % result)
except ZeroDivisionError:
self.lineEdit.setText("除数不能为零")
except Exception as e:
self.lineEdit.setText(str(e))
if __name__=='__main__':
app = QApplication(sys.argv)
win = Mywindow()
win.pushButton_7.clicked.connect(win.on_btnClear_clicked)
win.pushButton.clicked.connect(win.on_btn1_clicked)
win.pushButton_2.clicked.connect(win.on_btn2_clicked)
win.pushButton_3.clicked.connect(win.on_btn3_clicked)
win.pushButton_4.clicked.connect(win.on_btn_plus_clicked)
win.pushButton_5.clicked.connect(win.on_btn4_clicked)
win.pushButton_6.clicked.connect(win.on_btn7_clicked)
win.pushButton_8.clicked.connect(win.on_btn5_clicked)
win.pushButton_9.clicked.connect(win.on_btn8_clicked)
win.pushButton_10.clicked.connect(win.on_btn0_clicked)
win.pushButton_11.clicked.connect(win.on_btn6_clicked)
win.pushButton_12.clicked.connect(win.on_btn9_clicked)
win.pushButton_13.clicked.connect(win.on_btnequal_clicked)
win.pushButton_14.clicked.connect(win.on_btn_sub_clicked)
win.pushButton_15.clicked.connect(win.on_btn_mul_clicked)
win.pushButton_16.clicked.connect(win.on_btn_div_clicked)
win.show()
sys.exit(app.exec_())
```
希望能帮到你!
阅读全文