• PyQt4 UI设计和调用 使用eric6


    使用工具eric6

    安装包地址:

    http://eric-ide.python-projects.org/eric-download.html
    

    1.需要安装python和pyqt为前提,然后安装eric
    2.安装好后在 C:Python27Libsite-packageseric6 路径下找到 eric6.pyw ,打开eric6
    3.第一次打开需要设置API等参数

    4.使用qt编辑器开发ui界面

    5.在eric内ui文件上右键编译窗体
    6.生成对话框代码
    计算器测试程序

    # -*- coding: utf-8 -*-
    
    """
    Module implementing Dialog_calc.
    """
    
    from PyQt4.QtCore import pyqtSignature
    from PyQt4.QtGui import QDialog
    
    from Ui_UI_calc import Ui_Dialog
    
    from PyQt4 import QtCore, QtGui
    import sys
    
    class Dialog_calc(QDialog, Ui_Dialog):
        """
        Class documentation goes here.
        """
        lcdstring = ''
        operation = ''
        currentNum = 0
        prevlousNum = 0
        result = 0
        
        def __init__(self, parent=None):
            """
            Constructor
            
            @param parent reference to the parent widget
            @type QWidget
            """
            QDialog.__init__(self, parent)
            self.setupUi(self)
            
            self.action()
            
        def action(self):
            #定义信号和槽关系
            #按下数字执行的方法
            self.connect(self.pb0, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb1, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb2, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb3, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb4, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb5, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb6, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb7, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb8, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb9, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            self.connect(self.pb_point, QtCore.SIGNAL('clicked()'), self.buttonClicked)
            #按下操作执行的方法
            self.connect(self.pb_plus, QtCore.SIGNAL('clicked()'), self.opClicked)
            self.connect(self.pb_sub, QtCore.SIGNAL('clicked()'), self.opClicked)
            self.connect(self.pb_mul, QtCore.SIGNAL('clicked()'), self.opClicked)
            self.connect(self.pb_div, QtCore.SIGNAL('clicked()'), self.opClicked)
            #按下清除执行的方法
            self.connect(self.pb_clear, QtCore.SIGNAL('clicked()'), self.clearClicked)
            #按下等于号执行的方法
            self.connect(self.pb_eq, QtCore.SIGNAL('clicked()'), self.eqClicked)
        
        def buttonClicked(self):
            #最多显示8位
            # print len(Dialog_calc.lcdstring)
            if len(Dialog_calc.lcdstring) <= 7 :
    
                Dialog_calc.lcdstring += self.sender().text()
    
    
                #第一个输入的是小数点时候替替换为0.
                if Dialog_calc.lcdstring == '.':
                    Dialog_calc.lcdstring = '0.'
                #去掉多次输入小数点时候的错误
                elif str(Dialog_calc.lcdstring).count('.') > 1:
                    Dialog_calc.lcdstring = str(Dialog_calc.lcdstring)[:-1]
    
    
                self.lcd.display(Dialog_calc.lcdstring)
                Dialog_calc.currentNum = float(Dialog_calc.lcdstring)
          
        def opClicked(self):
            Dialog_calc.prevlousNum = Dialog_calc.currentNum
            Dialog_calc.currentNum = 0
            Dialog_calc.lcdstring = ''
            Dialog_calc.operation = self.sender().objectName()
        
        def clearClicked(self):
            Dialog_calc.lcdstring = ''
            Dialog_calc.operation = ''
            Dialog_calc.currentNum = 0
            Dialog_calc.prevlousNum = 0
            Dialog_calc.result = 0
            self.lcd.display('0')
        
        def eqClicked(self):
            if Dialog_calc.operation == 'pb_plus':
                Dialog_calc.result = Dialog_calc.prevlousNum + Dialog_calc.currentNum
                self.lcd.display(Dialog_calc.result)
            elif Dialog_calc.operation == 'pb_sub':
                Dialog_calc.result = Dialog_calc.prevlousNum - Dialog_calc.currentNum
                self.lcd.display(Dialog_calc.result)
            elif Dialog_calc.operation == 'pb_mul':
                Dialog_calc.result = Dialog_calc.prevlousNum * Dialog_calc.currentNum
                self.lcd.display(Dialog_calc.result)
            elif Dialog_calc.operation == 'pb_div':
                if Dialog_calc.currentNum == 0:
                    self.lcd.display('Error')
                    Dialog_calc.result = 0
                else:
                    Dialog_calc.result = Dialog_calc.prevlousNum / Dialog_calc.currentNum
                    self.lcd.display(Dialog_calc.result)
    
            Dialog_calc.currentNum = Dialog_calc.result
    
            Dialog_calc.lcdstring = ''
    
        def closeEvent(self, QCloseEvent):
            reply = QtGui.QMessageBox.question(self,u'警告',u'是否退出',QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.Yes:
                QCloseEvent.accept()
            else:
                QCloseEvent.ignore()
    
    
            
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        mycalc = Dialog_calc()
        mycalc.show()
        sys.exit(app.exec_())
    
    

    7.F2运行代码,当然也可以用PyCharm去写,比较习惯点。

  • 相关阅读:
    安卓系统隐藏滚动条
    jquery 失去焦点时输入框为空时自动填写默认内容
    移动端左右滑动代码
    [转]C#使用FFmpeg 将视频格式转换成MP4示例
    [转]Windows 使用ffmpeg将MP4视频转换为m3u8格式
    [转]搭建flutter开发环境
    [转]ios webview下纯JS实现长按
    [转]SQLServer查询数据库各种历史记录
    [转]Java多线程学习(总结很详细!!!)
    [转]安卓加固之so文件加固
  • 原文地址:https://www.cnblogs.com/Mysterious/p/7624693.html
Copyright © 2020-2023  润新知