# -*- coding: utf-8 -*-
# 导入相关库
from PyQt5.QtWidgets import QApplication ,QMainWindow
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(280, 20, 371, 81))
self.label.setStyleSheet("font: 28pt \"Agency FB\";")
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 420, 241, 121))
self.pushButton.setStyleSheet("font: 18pt \"Agency FB\";")
self.pushButton.setObjectName("pushButton")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(210, 260, 421, 61))
self.textBrowser.setStyleSheet("font: 26pt \"Agency FB\";")
self.textBrowser.setObjectName("textBrowser")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(210, 150, 421, 61))
self.textEdit.setStyleSheet("font: 26pt \"Agency FB\";")
self.textEdit.setObjectName("textEdit")
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(240, 350, 101, 31))
self.checkBox.setObjectName("checkBox")
self.checkBox_2 = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_2.setGeometry(QtCore.QRect(410, 350, 101, 31))
self.checkBox_2.setObjectName("checkBox_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "不太万能的计算器"))
self.pushButton.setText(_translate("MainWindow", "按钮"))
self.checkBox.setText(_translate("MainWindow", "计算他的平方"))
self.checkBox_2.setText(_translate("MainWindow", "计算他的立方"))
#当按钮被点击时时
def button_clicked():
# 添加全局变量
global input_false
# 检查勾选哪个按钮
check_1 = ui.checkBox.isChecked()
check_2 = ui.checkBox_2.isChecked()
num = ui.textEdit.toPlainText()
# 检查输入的是否全为数字
if num.isdigit():
num = int(num)
# 输入是否错误(后边用来提示)
input_false = 0
else:
check_1 = False
check_2 = False
# 输入是否错误,后边提示用
input_false = 1
# 当两个被同时勾选时
if check_1 and check_2:
ui.textBrowser.setText("只能勾选一个")
# 平方按钮被勾选时
elif check_1:
result = num ** 2
result = str(result)
ui.textBrowser.setText(result)
# 立方按钮被勾选时
elif check_2:
result = num ** 3
result = str(result)
ui.textBrowser.setText(result)
# 当输入框没有输入数字时
elif input_false == 1:
ui.textBrowser.setText("请输入数字")
# 当两个都没有被勾选时
else:
ui.textBrowser.setText("请勾选计算平方或立方")
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
# 讲按钮点击事件与函数关联起来
ui.pushButton.clicked.connect(button_clicked)
sys.exit(app.exec_())
此源码为直播时讲解的源码,我加了注释文章来源地址https://www.toymoban.com/news/detail-676974.html
文章来源:https://www.toymoban.com/news/detail-676974.html
到了这里,关于Python使用PyQt5实现计算平方或者立方的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!