您的位置:首页 > 编程语言 > Qt开发

Study PyQt from beginning 之 2

2013-12-11 12:38 525 查看

用下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,可以执行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息