Alex的博客

本博客的文章均为原创,是本人从事行业多年来所遇见一些小问题的解决心得,希望可以帮助到大家!



pyqt 简易登录界面,以及界面之间的传值

bin/LoginUI.py

# -*- coding: utf-8 -*-  # Form implementation generated from reading ui file 'LoginUI.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost!  from PyQt4 import QtCore, QtGui try:
    _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try:
    _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_LoginForm(object): def setupUi(self, LoginForm):
        LoginForm.setObjectName(_fromUtf8("LoginForm"))
        LoginForm.resize(400, 300)
        LoginForm.setMinimumSize(QtCore.QSize(400, 300))
        LoginForm.setMaximumSize(QtCore.QSize(400, 300))
        LoginForm.setBaseSize(QtCore.QSize(400, 300)) self.UserNamelabel = QtGui.QLabel(LoginForm) self.UserNamelabel.setGeometry(QtCore.QRect(90, 30, 54, 12)) self.UserNamelabel.setObjectName(_fromUtf8("UserNamelabel")) self.Passwdlabel = QtGui.QLabel(LoginForm) self.Passwdlabel.setGeometry(QtCore.QRect(90, 80, 54, 12)) self.Passwdlabel.setObjectName(_fromUtf8("Passwdlabel")) self.Loginbtn = QtGui.QPushButton(LoginForm) self.Loginbtn.setGeometry(QtCore.QRect(120, 160, 75, 23)) self.Loginbtn.setObjectName(_fromUtf8("Loginbtn")) self.Closebtn = QtGui.QPushButton(LoginForm) self.Closebtn.setGeometry(QtCore.QRect(240, 160, 75, 23)) self.Closebtn.setObjectName(_fromUtf8("Closebtn")) self.UserNamelineEdit = QtGui.QLineEdit(LoginForm) self.UserNamelineEdit.setGeometry(QtCore.QRect(150, 20, 151, 31)) self.UserNamelineEdit.setObjectName(_fromUtf8("UserNamelineEdit")) self.lineEdit_2 = QtGui.QLineEdit(LoginForm) self.lineEdit_2.setGeometry(QtCore.QRect(150, 70, 151, 31)) self.lineEdit_2.setEchoMode(QtGui.QLineEdit.Password) self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2")) self.retranslateUi(LoginForm)
        QtCore.QObject.connect(self.Closebtn, QtCore.SIGNAL(_fromUtf8("clicked()")), LoginForm.close)
        QtCore.QMetaObject.connectSlotsByName(LoginForm) def retranslateUi(self, LoginForm):
        LoginForm.setWindowTitle(_translate("LoginForm", "北京卡来宝科技信息有限公司", None)) self.UserNamelabel.setText(_translate("LoginForm", "用户名", None)) self.Passwdlabel.setText(_translate("LoginForm", "密码", None)) self.Loginbtn.setText(_translate("LoginForm", "登陆", None)) self.Closebtn.setText(_translate("LoginForm", "取消", None)) if __name__ == "__main__": import sys
    app = QtGui.QApplication(sys.argv)
    LoginForm = QtGui.QWidget()
    ui = Ui_LoginForm()
    ui.setupUi(LoginForm)
    LoginForm.show()
    sys.exit(app.exec_())
bin/productUI.py
# -*- coding: utf-8 -*-  # Form implementation generated from reading ui file 'ProductUI.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost!  from PyQt4 import QtCore, QtGui try:
    _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try:
    _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_ProductForm(object): def setupUi(self, ProductForm):
        ProductForm.setObjectName(_fromUtf8("ProductForm"))
        ProductForm.resize(600, 400)
        ProductForm.setMinimumSize(QtCore.QSize(600, 400))
        ProductForm.setMaximumSize(QtCore.QSize(600, 400))
        ProductForm.setBaseSize(QtCore.QSize(600, 400)) self.Productlabel = QtGui.QLabel(ProductForm) self.Productlabel.setGeometry(QtCore.QRect(50, 80, 54, 12)) self.Productlabel.setObjectName(_fromUtf8("Productlabel")) self.Qudaolabel = QtGui.QLabel(ProductForm) self.Qudaolabel.setGeometry(QtCore.QRect(50, 110, 54, 12)) self.Qudaolabel.setObjectName(_fromUtf8("Qudaolabel")) self.Rightlabel = QtGui.QLabel(ProductForm) self.Rightlabel.setGeometry(QtCore.QRect(50, 140, 54, 12)) self.Rightlabel.setObjectName(_fromUtf8("Rightlabel")) self.Commitbtn = QtGui.QPushButton(ProductForm) self.Commitbtn.setGeometry(QtCore.QRect(170, 230, 75, 23)) self.Commitbtn.setObjectName(_fromUtf8("Commitbtn")) self.Cancelbtn = QtGui.QPushButton(ProductForm) self.Cancelbtn.setGeometry(QtCore.QRect(270, 230, 75, 23)) self.Cancelbtn.setObjectName(_fromUtf8("Cancelbtn")) self.QudaolineEdit = QtGui.QLineEdit(ProductForm) self.QudaolineEdit.setGeometry(QtCore.QRect(130, 110, 151, 20)) self.QudaolineEdit.setObjectName(_fromUtf8("QudaolineEdit")) self.ProductcomboBox = QtGui.QComboBox(ProductForm) self.ProductcomboBox.setGeometry(QtCore.QRect(130, 80, 151, 22)) self.ProductcomboBox.setObjectName(_fromUtf8("ProductcomboBox")) self.ProductcomboBox.addItem(_fromUtf8("")) self.ProductcomboBox.addItem(_fromUtf8("")) self.RightcomboBox = QtGui.QComboBox(ProductForm) self.RightcomboBox.setGeometry(QtCore.QRect(130, 140, 151, 22)) self.RightcomboBox.setObjectName(_fromUtf8("RightcomboBox")) self.RightcomboBox.addItem(_fromUtf8("")) self.RightcomboBox.addItem(_fromUtf8("")) self.RightcomboBox.addItem(_fromUtf8("")) self.RightcomboBox.addItem(_fromUtf8("")) self.retranslateUi(ProductForm)
        QtCore.QObject.connect(self.Cancelbtn, QtCore.SIGNAL(_fromUtf8("clicked()")), ProductForm.close)
        QtCore.QMetaObject.connectSlotsByName(ProductForm) def retranslateUi(self, ProductForm):
        ProductForm.setWindowTitle(_translate("ProductForm", "产品渠道信息", None)) self.Productlabel.setText(_translate("ProductForm", "产品类型", None)) self.Qudaolabel.setText(_translate("ProductForm", "渠道", None)) self.Rightlabel.setText(_translate("ProductForm", "权限", None)) self.Commitbtn.setText(_translate("ProductForm", "提交", None)) self.Cancelbtn.setText(_translate("ProductForm", "取消", None)) self.ProductcomboBox.setItemText(0, _translate("ProductForm", "车险", None)) self.ProductcomboBox.setItemText(1, _translate("ProductForm", "分期", None)) self.RightcomboBox.setItemText(0, _translate("ProductForm", "钻石", None)) self.RightcomboBox.setItemText(1, _translate("ProductForm", "黄金", None)) self.RightcomboBox.setItemText(2, _translate("ProductForm", "白银", None)) self.RightcomboBox.setItemText(3, _translate("ProductForm", "青铜", None)) if __name__ == "__main__": import sys
    app = QtGui.QApplication(sys.argv)
    ProductForm = QtGui.QWidget()
    ui = Ui_ProductForm()
    ui.setupUi(ProductForm)
    ProductForm.show()
    sys.exit(app.exec_())

bin/Qrcode.py


# -*- coding: utf-8 -*-  # Form implementation generated from reading ui file 'QrcodeUI.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost!  from PyQt4 import QtCore, QtGui try:
    _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try:
    _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_QRForm(object): def setupUi(self, QRForm):
        QRForm.setObjectName(_fromUtf8("QRForm"))
        QRForm.resize(350, 350)
        QRForm.setMinimumSize(QtCore.QSize(350, 350))
        QRForm.setMaximumSize(QtCore.QSize(350, 350))
        QRForm.setBaseSize(QtCore.QSize(350, 350)) self.IDlabel = QtGui.QLabel(QRForm) self.IDlabel.setGeometry(QtCore.QRect(20, 40, 54, 12)) self.IDlabel.setObjectName(_fromUtf8("IDlabel")) self.IDlineEdit = QtGui.QLineEdit(QRForm) self.IDlineEdit.setGeometry(QtCore.QRect(70, 29, 221, 31)) self.IDlineEdit.setObjectName(_fromUtf8("IDlineEdit")) self.groupBox = QtGui.QGroupBox(QRForm) self.groupBox.setGeometry(QtCore.QRect(20, 110, 311, 211)) self.groupBox.setObjectName(_fromUtf8("groupBox")) self.label = QtGui.QLabel(self.groupBox) self.label.setGeometry(QtCore.QRect(30, 30, 251, 171)) self.label.setText(_fromUtf8("")) self.label.setObjectName(_fromUtf8("label")) self.retranslateUi(QRForm)
        QtCore.QMetaObject.connectSlotsByName(QRForm) def retranslateUi(self, QRForm):
        QRForm.setWindowTitle(_translate("QRForm", "二维码", None)) self.IDlabel.setText(_translate("QRForm", "渠道id", None)) self.groupBox.setTitle(_translate("QRForm", "二维码", None)) if __name__ == "__main__": import sys
    app = QtGui.QApplication(sys.argv)
    QRForm = QtGui.QWidget()
    ui = Ui_QRForm()
    ui.setupUi(QRForm)
    QRForm.show()
    sys.exit(app.exec_())

主文件:

Main.py

#__author__ = 'shmpc' # -*- coding: utf-8 -*-  # Form implementation generated from reading ui file 'LoginUI.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost!  import sys import qrcode from bin.LoginUI import * from bin.ProductUI import * from bin.QrcodeUI import * from PyQt4 import QtCore, QtGui try:
    _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try:
    _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class FirstUI(QtGui.QDialog): def __init__(self,parent=None): super(FirstUI,self).__init__(parent) self.LoginForm = Ui_LoginForm() self.LoginForm.setupUi(self) self.connect(self.LoginForm.Loginbtn,QtCore.SIGNAL("clicked()"),self.LoginAction) def LoginAction(self):
        username = unicode(self.LoginForm.UserNamelineEdit.text().toUtf8(),'utf8','ignore').encode('utf-8')
        passwd = unicode(self.LoginForm.lineEdit_2.text().toUtf8(),'utf8','ignore').encode('utf-8') if username == '' or passwd == '':
            QtGui.QMessageBox.information(self,_fromUtf8("信息提示"),_fromUtf8("用户名或者密码不能为空")) else: self.accept() self.productForm = SecondUI()#需要self实例化为全局变量,不加self的话一运行就会被回收  self.productForm.show() class SecondUI(QtGui.QDialog): def __init__(self,parent=None): #super(SecondUI,self).__init__(parent)  QtGui.QDialog.__init__(self,parent) self.productUI = Ui_ProductForm() self.productUI.setupUi(self) self.connect(self.productUI.Commitbtn,QtCore.SIGNAL(_fromUtf8("clicked()")),self.commit) def commit(self):
        id = unicode(self.productUI.QudaolineEdit.text().toUtf8(),'utf8','ignore').encode("utf-8") if id == "" :
            QtGui.QMessageBox.information(self,_fromUtf8("提示"),_fromUtf8("请输入渠道码")) else : self.accept()
            id = self.productUI.QudaolineEdit.text() self.thirdUI = ThirdUI(Id=id) self.thirdUI.MQrcode(Id=id) self.thirdUI.show() class ThirdUI(QtGui.QDialog): def __init__(self,parent=None,Id=None): super(ThirdUI,self).__init__(parent) self.QrcodeUI = Ui_QRForm() self.QrcodeUI.setupUi(self) def MQrcode(self,Id=None):
        id =Id
        filename= "kalaibao.png"  qr = qrcode.QRCode( version=None,  error_correction=qrcode.constants.ERROR_CORRECT_L,  box_size=5 ,  border=4,  )
        qr.add_data(Id)
        qr.make(fit=True)
        img = qr.make_image()
        img.save(filename) self.QrcodeUI.label.setPixmap(QtGui.QPixmap(filename)) self.QrcodeUI.IDlineEdit.setText(id) if __name__ == "__main__":

    app=QtGui.QApplication(sys.argv)
    main = FirstUI()
    main.show()
    sys.exit(app.exec_())
浏览30  评论0  Alex于 2017-11-16 9:53
发言