zoukankan      html  css  js  c++  java
  • Python Rapid GUI Programming

    Python Rapid GUI Programming 第二篇。 30行写一个更奇葩的计算器。Python 基础教程

     

    接上一篇GUI编程的日志,现在我们来写一个正常点程序。先让我们看一下程序的样子。

    image

    看似正常多了。我们有了一个框框,一个X。而且不需要命令行输入了!

    根据上一篇日志所述,我们需要载入模块。

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

    from __future__ import division     #精确除法
    import sys
    from math import *
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

    根据截图,这个应用程序用了两个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_()
    复制代码
  • 相关阅读:
    实用产品规划
    产品经理对用户的调研
    产品经理用户研究
    竞品分析方案
    产品竞品分析
    Mybatis Plus
    shiro
    Spring cloud
    Spring Boot
    Redis入门(二)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2915290.html
Copyright © 2011-2022 走看看