找回密码
 立即注册
Qt开源社区 门户 查看内容

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

2019-2-9 10:12| 发布者: admin| 查看: 1366| 评论: 0

摘要: 作者:速学Python 点击蓝字“速学python”关注我们哟!今天来看一下如何来使用python设计一个属于自己的计算器,哈哈,python的gui还是蛮强的哦~~下面开始吧!先上截图哈!先载入QT4所用的模块以及计算所用的math模 ...
作者:速学Python


点击蓝字“速学python”关注我们哟!



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

先上截图哈!



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

from __future__ import division     #精确除法

import sys

from math import *

from PyQt4.QtCore import *

from PyQt4.QtGui import *



根据QT的规则,所有的字符都为Uni编码。根据截图,这个应用程序用了两个widgets ,一个是QTextBrowser这是一个只读的文本或者HTML查看器, 另一个是QLineEdit 是一个单行的可写的文本查看器。

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_()

一个计算机小程序就结束了,实践的过程中难免会遇到一些细小的问题,除了细心富有逻辑性还需要耐心哦!相信这么一个小程序,你很快就学会了。


今天就聊到这里啦,大家记得点赞收藏,分享转发,关注小姐姐哟!

END

更多python相关资料


直接扫描下方的微信二维码

来撩我们炒鸡可爱的无双老师



-------------------------------------------------------------------------
我们尊重原创,也注重分享,如若侵权请联系qter@qter.org。
-------------------------------------------------------------------------

鲜花

握手

雷人

路过

鸡蛋

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了