PyQt4 UI設計和調用 使用eric6

使用工具eric6

安裝包地址:html

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

1.須要安裝python和pyqt爲前提,而後安裝eric
2.安裝好後在 C:\Python27\Lib\site-packages\eric6 路徑下找到 eric6.pyw ,打開eric6
3.第一次打開須要設置API等參數

4.使用qt編輯器開發ui界面

5.在eric內ui文件上右鍵編譯窗體
6.生成對話框代碼
計算器測試程序python

# -*- 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去寫,比較習慣點。
app

相關文章
相關標籤/搜索