0

我在尝试使用 PyQt5 和 QT 设计器开发 GUI 时遇到问题。

我想使用该uic模块,我有一个.ui名为 的文件registro.ui,还有另一个login.ui,因为我可以login.ui通过registry.ui单击按钮调用registro

我也分别有Login.py,Registro.py 文件

我的意图是点击注册按钮,然后打开注册窗口

只使用uic.loadUI()方法?

在这里我展示我的代码

班级登录 login.py

import sys
import time
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5 import uic
import Con_pos
from Registro import Registro
class Login(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        uic.loadUi("Login.ui", self)
        self.registro.clicked.connect(self.view_Registro)
        self.ingresar.clicked.connect(self.autenticar)

    def autenticar(self):
        pid = self.getPID()
        con = Con_pos.con_pos()
        user = self.in_login.text()
        password = self.in_pass.text()
        print(password)
        fecha = time.strftime("%X") + " " + time.strftime("%x")
        if user != "" and password != "":
            consulta = ''' SELECT "id_user" FROM "User" WHERE "id_user" = '%s' ; ''' %(user)
            c_user = con.consulta(consulta)
            print(c_user)
            consulta = ''' SELECT "pass" FROM "User" WHERE "id_user" = '%s' ;''' %(user)
            c_pass = con.consulta(consulta)
            if c_user != None:
                c_pass = c_pass[0]
                print("test")
                print(c_pass)
                if c_pass == password:
                    consulta = ''' INSERT INTO "sesion" VALUES ('%s', '%s', '%s'); '''%(fecha ,pid, user)
                    con.insertar(consulta)
    def getPID(self):
        con = Con_pos.con_pos()
        func = "pg_backend_pid"
        pid = con.call_proc(func)
        pid = pid[0][0]
        return pid

    def view_Registro(self):
        reg = Registro.Registro()
        reg.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    login = Login()
    login.show()
    sys.exit(app.exec())

类注册: Registro.py

import sys
import time
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic
import Con_pos
import error_log as error

class Registro(QMainWindow):
    con = Con_pos.con_pos()
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Registro.ui", self)
        self.btn_reg.clicked.connect(self.validar_nombre)

    def validar_nombre(self):
        fecha = time.strftime("%X") + " " + time.strftime("%x")
        usuario = self.user_tx_input.text()
        password = self.pass_tx_input.text()
        if usuario != "":
            pid = self.getPID()
            consul = '''SELECT "id_user" FROM "User" WHERE "id_user" = '%s'; ''' % (usuario)
            res_con = Registro.con.consulta(consul)

            if not res_con:
                ins_user = '''INSERT INTO "User" VALUES ('%s', '%s') ''' % (usuario, password)
                ins_sesion = ''' INSERT INTO "sesion" VALUES ('%s', %s,'%s') ''' % (fecha, pid, usuario)
                Registro.con.insertar(ins_user)
                Registro.con.insertar(ins_sesion)
            else:
                err = error.error_dia()
                err.etiqueta.setText("El usuario %s ya esta en uso" % (usuario))
                err.exec()

    def getPID(self):
        func = "pg_backend_pid"
        pid = Registro.con.call_proc(func)
        pid = pid[0][0]
        return pid

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ventana = Registro()
    ventana.show()
    sys.exit(app.exec())
4

1 回答 1

0

您必须将参数作为父级传递给新窗口,您可以通过两种方式进行:

  1. 第一个解决方案:使变量 reg 成为类的属性:

def view_Registro(self):
    self.reg = Registro()
    self.reg.show()
  1. 第二种解决方案:作为参数传递给同一个实例:

def view_Registro(self):
    reg = Registro(self)
    reg.show()

对于此解决方案,您必须更改:

class Registro(QMainWindow):
    con = Con_pos.con_pos()
    def __init__(self):
        QMainWindow.__init__(self)

class Registro(QMainWindow):
    con = Con_pos.con_pos()
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
于 2017-04-20T16:52:06.653 回答