【问题标题】:Blank window using PyQt4使用 PyQt4 的空白窗口
【发布时间】:2018-08-09 21:21:06
【问题描述】:

我的程序目前有问题,当我运行程序时,会显示一个空白窗口。

它将显示我设置的标题。但是,标签、行编辑和按钮无法显示。我是 PYQT4 和 OOP 的新手,所以我不太确定我是否使用了正确的类,或者即使我需要为每个项目设置对象名称。

import sys
from PyQt4 import QtCore, QtGui
import mysql.connector

class RegisterPage(QtGui.QMainWindow):
  def __init__(self):
    super(RegisterPage, self).__init__()
    self.setupUi()

    self.setGeometry(0,0,640,480)
    self.setWindowTitle("Register")

  def setupUi(self):

    #this part of the code is for the title of the of the register page
    pg4titleLbl = QtGui.QLabel()
    pg4titleLbl.setGeometry(QtCore.QRect(250, 50, 150, 40))
    font = QtGui.QFont()
    font.setPointSize(26)
    font.setBold(True)
    font.setWeight(75)
    pg4titleLbl.setFont(font)
    pg4titleLbl.setObjectName("pg4titleLbl")
    pg4titleLbl.setText("Register")

    #this part of the code is the button to take the user back to the homepage
    HomePageLink = QtGui.QPushButton()
    HomePageLink.setGeometry(QtCore.QRect(0, 170, 60, 120))
    HomePageLink.setObjectName("HomePageLink")
    HomePageLink.setText("Home")

    #this part of the code is to let the users know they need to enter a username
    enterUsernameLbl = QtGui.QLabel()
    enterUsernameLbl.setGeometry(QtCore.QRect(90, 130, 190, 40))
    font = QtGui.QFont()
    font.setPointSize(16)
    enterUsernameLbl.setFont(font)
    enterUsernameLbl.setObjectName("enterUsernameLbl")
    enterUsernameLbl.setText("Choose a username:")

    #this part of the code is there so the user can input their username they want to create
    usernameInput = QtGui.QLineEdit()
    usernameInput.setGeometry(QtCore.QRect(290, 140, 260, 30))
    usernameInput.setObjectName("UsernameInput")

    #this part of the code is to let the user know that they need to enter an email
    createEmailLbl = QtGui.QLabel()
    createEmailLbl.setGeometry(QtCore.QRect(130, 180, 160, 40))
    font = QtGui.QFont()
    font.setPointSize(16)
    createEmailLbl.setFont(font)
    createEmailLbl.setObjectName("createEmailLbl")
    createEmailLbl.setText("Create an Email:")

    #this part of the code is there so the user can input their email they want to use
    createEmailInput = QtGui.QLineEdit()
    createEmailInput.setGeometry(QtCore.QRect(290, 190, 260, 30))
    createEmailInput.setObjectName("createEmailInput")

    #this part of the code is to let the user know that they need to make a password
    createPaswordLbl = QtGui.QLabel()
    createPaswordLbl.setGeometry(QtCore.QRect(100, 230, 200, 40))
    font = QtGui.QFont()
    font.setPointSize(16)
    createPaswordLbl.setFont(font)
    createPaswordLbl.setObjectName("createPaswordLbl")
    createPaswordLbl.setText("Choose a Password:")

    #this part of the code is there so the user can input the password they want to create
    createPasswordInput = QtGui.QLineEdit()
    createPasswordInput.setGeometry(QtCore.QRect(290, 240, 260, 30))
    createPasswordInput.setObjectName("createPasswordInput")

    #this part of the of the code is to let the user know that they need to re-enter the password they just created
    confirmPasswordLbl = QtGui.QLabel()
    confirmPasswordLbl.setGeometry(QtCore.QRect(110, 280, 200, 40))
    font = QtGui.QFont()
    font.setPointSize(16)
    confirmPasswordLbl.setFont(font)
    confirmPasswordLbl.setObjectName("confirmPasswordLbl")
    confirmPasswordLbl.setText("Confirm password:")

    #this part of the code is there so the user can re-enter the password they just create
    confirmPasswordInput = QtGui.QLineEdit()
    confirmPasswordInput.setGeometry(QtCore.QRect(290, 290, 260, 30))
    confirmPasswordInput.setObjectName("confirmPasswordInput")

    #this button is used so the user can input there data into the system
    registerButton = QtGui.QPushButton()
    registerButton.setGeometry(QtCore.QRect(260, 350, 110, 30))
    registerButton.setObjectName("registerButton")
    registerButton.setText("Register")


    def register():
        if createPasswordInput.text()==confirmPasswordInput.text():
            password=confirmPasswordInput.text()
        else:
            msg = QtGui.QMessageBox.information(self, 'Message', 'Inputted passwords are not the same', QtGui.QMessageBox.Ok)
        email=createEmailInput.text()
        username=usernameInput.text()
        if password=="" or email=="" or username=="":
            msg = QtGui.QMessageBox.information(self, 'Message', 'please fill in all boxes', QtGui.QMessageBox.Ok)
        else:
            cnx = mysql.connector.connect(user='scott', password='password',host='127.0.0.1',database='employees')
            add_user = ("INSERT INTO draughtsUsers "
           "(username, passsword, email) "
           "VALUES (%s, %s, %s)")
            cursor=cnx.cursor()
            userData=(username,password,email)
            cursor.execute(add_user, userData)
            userId= cursor.lastrowid
            cnx.commit()
            cursor.close()
            cnx.close()


    #this button will take the user back to the login page if they already have an account
    loginPageLink = QtGui.QPushButton()
    loginPageLink.setGeometry(QtCore.QRect(390, 420, 240, 30))
    loginPageLink.setStyleSheet("QPushButton { border: 0px;\n} QPushButton:hover {color:rgb(0,0,225);}")
    loginPageLink.setObjectName("loginPageLink")
    loginPageLink.setText("If you already have an account, login here")


if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  window = RegisterPage()
  window.show()
  sys.exit(app.exec_())

【问题讨论】:

  • 成功了吗?你明白我的解决方案了吗?

标签: python pyqt pyqt4 qwidget qmainwindow


【解决方案1】:

不是顶层的小部件的位置相对于您的父级,但在您的情况下没有父级,解决方案是将self 作为父级传递给他。

def setupUi(self):
    pg4titleLbl = QtGui.QLabel(self)
    [...]   
    HomePageLink = QtGui.QPushButton(self)
    [...]  
    enterUsernameLbl = QtGui.QLabel(self)
    [...]  
    usernameInput = QtGui.QLineEdit(self)
    [...]  
    createEmailLbl = QtGui.QLabel(self)
    [...]  
    createEmailInput = QtGui.QLineEdit(self)
    [...]  
    createPaswordLbl = QtGui.QLabel(self)
    [...]  
    createPasswordInput = QtGui.QLineEdit(self)
    [...]  
    confirmPasswordLbl = QtGui.QLabel(self)
    [...]  
    confirmPasswordInput = QtGui.QLineEdit(self)
    [...]  
    registerButton = QtGui.QPushButton(self)
    [...]  

【讨论】:

  • @johnmaf23 如果您查看我的答案,您会看到我已将 self 添加到小部件构造函数中,您的代码:pg4titleLbl = QtGui.QLabel() ---> 我的代码:pg4titleLbl = QtGui.QLabel(self)
  • @johnmaf23 有用吗?你明白我的解决方案了吗?
  • @johnmaf23 如果我的回答对您有帮助,请不要忘记将其标记为正确,如果您不知道该怎么做,请查看tour
猜你喜欢
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多