• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

用python 30行设计一个属于自己的计算器

python 搞java代码 3年前 (2022-05-21) 26次浏览 已收录 0个评论

今天来看一下如何来使用python设计一个属于自己的计算器,哈哈,python的gui还是蛮强的哦~~下面开始吧

先上截图哈

先载入QT4所用的模块以及计算所用的math模块。

from __future__ <a href="https://www.gaodaima.com/tag/import" title="查看更多关于import的文章" target="_blank">import</a> division     #精确除法
import sys
from <a href="https://www.gaodaima.com/tag/math" title="查看更多关于math的文章" target="_blank">math</a> import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
www#gaodaima.com来源gaodai.ma#com搞##代!^码@网搞代码

根据截图,这个应用程序用了两个widgets ,一个是QTextBrowser这是一个只读的文本或者HTML查看器, 另一个是QLineEdit 是一个单行的可写的文本查看器。

根据QT的规则,所有的字符都为Uni编码。

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.connect(self.lineedit, SIGNAL("returnPressed()"),
                     self.updateUi)
        self.setWindowTitle("Calculate coding by Kaysin")

这样就完成了初始画面的定义。

QVBoxLayout()  就是一个可以放置widget的页面。

而下面的addWidget方法,就是将所创建的widget添加进新的页面。

下面有触发信号,按下回车。

载入函数 upadteUi

def updateUi(self):
        try:
            text = unicode(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)

这个很好理解,就是判断输入是否合法,出现异常则输出不合法。

我们看下源程序。

from __future__ import division
import sys
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
  
  
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.connect(self.lineedit, SIGNAL("returnPressed()"),
                     self.updateUi)
        self.setWindowTitle("Calculate coding by Kaysin")
  
    def updateUi(self):
        try:
            text = unicode(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)
  
  
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:用python 30行设计一个属于自己的计算器

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址