Study PyQt from beginning 之 2

用下Eric4

打開後,如果是第一次使用,就需要設置參數,重點是在各參數中選擇開發語言相關的爲python, 同時選擇工作目錄Workspace爲自己指定的路徑,系統默認是用戶根目錄,最好換個。

在Project菜單New一個

填上工程名等等OK了,注意這裏有一項 Main Script 意思是項目啓動運行的腳本文件,這暫時不知道先爲空。


只有注意項目視圖上,系統自動產生了一個空的__init__.py文件,和其他的項目控制文件(這些文件有些隱藏了,暫時不用關心)

OK,現在要用到QTDesigner了,要不然用它幹嘛。


點擊“Forms"圖標,右鍵“new form":

QTDesigner就出現了。就是因爲PyQt能用到QTDesigner,所以在做python的gui項目的時候,這個功能就非常方便。

隨便先做個登錄對話框,再出現一個主界面。

選擇沒有按鈕的Dialog,加上兩個Label,再加上兩個lineEdit, 修改name後,將passed的那個修改一下echoMode爲password


保存後退出QTDesigner,切換到Eric4,就可以看到Forms視圖中有這個ui文件了,鼠標右鍵compile Form,就會生成Ui_xxxx.py的文件。

同理,再增加一個Form爲mainwindow(Form Type選擇MainWindow),先空白板,保存後compile,最後Eric4的項目文件視圖爲3個py文件:

打開看看生成的Ui_xxx.py發現在Compile的時候,都生成了如下代碼段:



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

那麼,兩個Ui_xxx.py 有兩段main,不是想要的,於是新增一個py文件,將main 的部分寫入這個新增的py文件,並設置這個Project,將Main Script設置爲新的這個主程序。

主程序代碼如下:

# -*- coding: utf-8 -*-


from Ui_Login import *
from Ui_MainWorkPanel import *


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

main的部分是從Ui_Login.py裏面拷貝過來的,只是在文件前面添加了3行,頭一行是說明本文件是utf8格式文件,有中文情況下,本句必須。然後就是from xxx import *,添加兩個由QTDesigner生成的py文件的引用。設置完MainScript之後,執行,發現那個Login Dialog顯示出來了。


退出的時候會報錯:

2013-12-11 11:41:25.101 eric[1672:507] modalSession has been exited prematurely - check for a reentrant call to endModalSession:

貌似是Qt在MacOs 10.9的bug,暫時不影響先不管。


接下來,處理一下Login裏面的邏輯,當用戶在username按回車,就檢查輸入的長度,不可爲空,並焦點到password,如果在password回車,則退出Login Dialog,並顯示MainWindow

回到Forms視圖,在Login.ui鼠標右鍵 Generate Dialog Code,代碼生成窗口New一個類名,文件名默認。

之後要選擇要處理的控件信號,

選擇兩個lineEdit的 xx_returnPressed()事件,就是回車按下後的事件。系統會生成Login.py文件,打開修改。

# -*- coding: utf-8 -*-


"""
Module implementing LoginDialog.
"""


from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature


from Ui_Login import Ui_Dialog


class LoginDialog(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
    
    @pyqtSignature("")
    def on_lineEdit_passwd_returnPressed(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        raise NotImplementedError
    
    @pyqtSignature("")
    def on_lineEdit_username_returnPressed(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        raise NotImplementedError

修改TODO部分,註釋掉 raise ...,簡單修改下:

# -*- coding: utf-8 -*-


"""
Module implementing LoginDialog.
"""


from PyQt4.QtGui import QDialog, QMessageBox    
from PyQt4.QtCore import pyqtSignature


from Ui_Login import Ui_Dialog


class LoginDialog(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
    
    @pyqtSignature("")
    def on_lineEdit_passwd_returnPressed(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        #raise NotImplementedError
        msgBox = QMessageBox(self)
        username = self.lineEdit_username.text().trimmed()
        passwd = self.lineEdit_passwd.text().trimmed()
        
        if username.length() == 0:
            msgBox.setText(u'用戶名不能爲空')
            msgBox.exec_()
        elif passwd.length() == 0:
            msgBox.setText(u'密碼不能爲空')
            msgBox.exec_()
        elif passwd.length() < 3:
            self.reject()
        else:
            self.accept()




    
    @pyqtSignature("")
    def on_lineEdit_username_returnPressed(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        # raise NotImplementedError
        msgBox = QMessageBox(self)
        username = self.lineEdit_username.text().trimmed()
        if username.length() == 0:
            msgBox.setText(u'用戶名不能爲空')
            msgBox.exec_()
        else:
            self.lineEdit_passwd.setFocus()



    
    @pyqtSignature("")
    def on_lineEdit_username_returnPressed(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        # raise NotImplementedError
        msgBox = QMessageBox(self)
        username = self.lineEdit_username.text().trimmed()
        if username.length() == 0:
            msgBox.setText(u'用戶名不能爲空')
            msgBox.exec_()
        else:
            self.lineEdit_passwd.setFocus()


同時,修改主程序爲:

# -*- coding: utf-8 -*-


from PyQt4 import QtGui
from Login import *
from Ui_MainWorkPanel import *


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = LoginDialog()


    rst = Dialog.exec_()
        
    if rst != QtGui.QDialog.Accepted:
        QtGui.QMessageBox.warning(Dialog, u'問題', u'結果爲Reject')
    else:
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        
        
    sys.exit(app.exec_())

注意區別是 對話框是LoginDialog的實例,而不是自動生成的那個Ui_xxx的實例。

而else部分,是從ui_MainWorkPanel.py裏面抄過來的。


最妙的是,這時候無論怎麼修改ui文件,佈局什麼什麼的,重新compile form,都不會影響LoginDialog和主程序,實現了ui界面與程序邏輯的基本脫鉤。


OK,可以執行了。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章