如何用 Python 写一个带 GUI 的科学计算程序
1个回答
展开全部
import sys
from math import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QTextBrowser, QVBoxLayout
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.lineedit = QLineEdit("Type an expression and press Enter")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(layout)
self.lineedit.setFocus()
self.lineedit.returnPressed.connect(self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text = self.lineedit.text()
self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
except:
self.browser.append("<font color=red>%s is invalid!</font>" % text)
self.lineedit.setText('')
if __name__=="__main__":
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
运行这个程序就能得到一个小的计算器,前提你已经安装好了pyqt
python中的GUI可以利用pyqt进行编写
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询