我写了3类的代码。第一类是Ui_MainWindow
,显示主窗口,第二类是Ui_Dialog
,显示寄存器屏幕。第三类是他们联系的地方。如果您检查了第三堂课,您将看到他们的连接方式,我创建了一个self.screen_chosen("Log İn")
,因此输入了if语句并连接到Ui_MainWindow
,当我们单击登录按钮时,它连接到注册菜单,即{ {1}},此后,当我们单击“注册”按钮时,它与Ui_Dialog
和def ferry()
的连接正在第二次激活,因此这里的错误self.parent.screen_chosen("Log İn")
是因为self.MainWindow = QtWidgets.QMainWindow()
python崩溃后没有显示。请帮助
print("enter here 2 ")
if __name__ == "__main__":
print("enter here 1")
self.app = QtWidgets.QApplication(sys.argv)
self.MainWindow = QtWidgets.QMainWindow()
print("enter here 2")
self.ui = Ui_MainWindow(self)
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
sys.exit(self.app.exec_())
答案 0 :(得分:0)
要使该示例开始工作,您需要重写class Screen (object)
,如下所示:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
class Ui_MainWindow(object):
def __init__(self, parent):
self.parent = parent
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 596)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(410, 280, 90, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton2.setGeometry(QtCore.QRect(310, 280, 90, 23))
self.pushButton2.setObjectName("pushButton2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(280, 230, 47, 13))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(280, 200, 47, 13))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(360, 200, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(360, 230, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.onclick)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Sign İn"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.pushButton2.setText(_translate("MainWindow", "Log İn"))
def onclick(self):
self.parent.screen_chosen("Register")
class Ui_Dialog(object):
def __init__(self, parent):
self.parent = parent
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButton_2 = QtWidgets.QPushButton(self.frame)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout_2.addWidget(self.pushButton_2, 0, 0, 1, 1)
self.label = QtWidgets.QLabel(self.frame)
self.label.setObjectName("label")
self.gridLayout_2.addWidget(self.label, 1, 0, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout_2.addWidget(self.lineEdit, 1, 1, 1, 1)
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setObjectName("label_2")
self.gridLayout_2.addWidget(self.label_2, 2, 0, 1, 1)
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.gridLayout_2.addWidget(self.lineEdit_2, 2, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 3, 1, 1, 1)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.ferry)
self.con = sqlite3.connect("database.db")
self.cursor = self.con.cursor()
query = "Create Table if not Exists users (Nick TEXT, Password TEXT)"
self.cursor.execute(query)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_2.setText(_translate("Dialog", "back"))
self.label.setText(_translate("Dialog", "Nickname"))
self.label_2.setText(_translate("Dialog", "Password"))
self.pushButton.setText(_translate("Dialog", "Sign up"))
def ferry(self):
sorgu = "Select * From users where Nick = ?"
self.cursor.execute(sorgu,(self.lineEdit.text(),))
fetch = self.cursor.fetchall()
if len(fetch) != 0:
print("This user already exists")
else:
query = "INSERT INTO users Values(?,?)"
self.cursor.execute(query, (self.lineEdit.text(),self.lineEdit_2.text()))
self.con.commit()
if self.lineEdit.text().strip() == "" or self.lineEdit_2.text().strip() == "":
print("Please enter your Nick name and your password")
self.parent.screen_chosen("Log İn")
self.parent.Dialog.hide() # +++
class Screen(object):
def __init__(self):
self.screen_chosen("Log İn")
def screen_chosen(self, name):
if name == "Log İn":
# if __name__ == "__main__":
# self.app = QtWidgets.QApplication(sys.argv)
# app = QtWidgets.QApplication(sys.argv)
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow(self)
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
# sys.exit(app.exec_())
elif name == "Register":
# if __name__ == "__main__":
# self.app = QtWidgets.QApplication(sys.argv)
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog(self)
self.ui.setupUi(self.Dialog)
self.Dialog.show()
if __name__ == "__main__": # +++
app = QtWidgets.QApplication(sys.argv) # +++
screen = Screen()
sys.exit(app.exec_()) # +++