如何创建一个 window 来打开另一个带有 uic 模块的 window ?? Qt 设计器和 Python
How to create a window that opens another window with uic module ?? Qt-Designer and Python
我在尝试使用 PyQt5 和 QT 设计器开发 GUI 时遇到问题。
我想使用 uic
模块,我有一个名为 registro.ui
的 .ui
文件和另一个 login.ui
,我可以从 login.ui
到 registry.ui
,点击按钮 registro
我也分别有 Login.py
, Registro.py
个文件
我的意图是点击注册按钮,打开注册window
只使用uic.loadUI()
方法?
这里我展示我的代码
Class 登录
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())
Class 注册:
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())
您必须将一个参数作为父参数传递给新 window,您可以通过两种方式进行:
- 第一个解决方案:使变量reg成为class的属性:
def view_Registro(self):
self.reg = Registro()
self.reg.show()
- 第二种解决方案:作为参数传递给同一个实例:
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)
我在尝试使用 PyQt5 和 QT 设计器开发 GUI 时遇到问题。
我想使用 uic
模块,我有一个名为 registro.ui
的 .ui
文件和另一个 login.ui
,我可以从 login.ui
到 registry.ui
,点击按钮 registro
我也分别有 Login.py
, Registro.py
个文件
我的意图是点击注册按钮,打开注册window
只使用uic.loadUI()
方法?
这里我展示我的代码
Class 登录
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())
Class 注册:
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())
您必须将一个参数作为父参数传递给新 window,您可以通过两种方式进行:
- 第一个解决方案:使变量reg成为class的属性:
def view_Registro(self):
self.reg = Registro()
self.reg.show()
- 第二种解决方案:作为参数传递给同一个实例:
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)