R
R
Romacron2021-02-13 23:15:07
Python
Romacron, 2021-02-13 23:15:07

I want to add another QWidget to the program, how to do it right?

I started programming recently. I want to add another QtWidget to the program, which is a form for entering somewhere. existing widget. Below is the code with the Register button not working:

spoiler
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout, QMessageBox)


class LoginForm(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle('LoginForm')
    self.resize(500,120)

    layout = QGridLayout()

    label_name = QLabel('<font size = "4"> Username </font>')
    self.LineEdit_username = QLineEdit()
    self.LineEdit_username.setPlaceholderText('Enter your username:')
    layout.addWidget(label_name, 0, 0)
    layout.addWidget(self.LineEdit_username, 0, 1)

    label_name = QLabel('<font size = "4"> Password </font>')
    self.LineEdit_password = QLineEdit()
    self.LineEdit_password.setPlaceholderText('Enter your password:')
    layout.addWidget(label_name, 1, 0)
    layout.addWidget(self.LineEdit_password, 1, 1)

    button_login = QPushButton('Login')
    button_login.clicked.connect(self.check_password)
    layout.addWidget(button_login, 2, 0, 1, 2)
    layout.setRowMinimumHeight(2, 75)

    button_register = QPushButton('Register')
    #button_register.clicked.connect(self.registration_form)
    layout.addWidget(button_register, 3, 0, 1,2)
    layout.setRowMinimumHeight(1, 75)



    self.setLayout(layout)

  def check_password(self):
    msg = QMessageBox()

    if self.LineEdit_username.text() == 'Roman' and self.LineEdit_password.text() == '1234':
      msg.setText('Success')
      msg.exec_()
      app.quit()
    else:
      msg.setText('Error')
      msg.exec_()



      

if __name__ == '__main__':
  app = QApplication(sys.argv)

  form = LoginForm()
  form.show()

  sys.exit(app.exec_())


So I tried to add another widget to the code, but it didn't work:
spoiler
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout, QMessageBox)


class LoginForm(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle('LoginForm')
    self.resize(500,120)

    layout = QGridLayout()

    label_name = QLabel('<font size = "4"> Username </font>')
    self.LineEdit_username = QLineEdit()
    self.LineEdit_username.setPlaceholderText('Enter your username:')
    layout.addWidget(label_name, 0, 0)
    layout.addWidget(self.LineEdit_username, 0, 1)

    label_name = QLabel('<font size = "4"> Password </font>')
    self.LineEdit_password = QLineEdit()
    self.LineEdit_password.setPlaceholderText('Enter your password:')
    layout.addWidget(label_name, 1, 0)
    layout.addWidget(self.LineEdit_password, 1, 1)

    button_login = QPushButton('Login')
    button_login.clicked.connect(self.check_password)
    layout.addWidget(button_login, 2, 0, 1, 2)
    layout.setRowMinimumHeight(2, 75)

    button_register = QPushButton('Register')
    button_register.clicked.connect(self.registration_form)
    layout.addWidget(button_register, 3, 0, 1,2)
    layout.setRowMinimumHeight(1, 75)



    self.setLayout(layout)

  def check_password(self):
    msg = QMessageBox()

    if self.LineEdit_username.text() == 'Roman' and self.LineEdit_password.text() == '1234':
      msg.setText('Success')
      msg.exec_()
      app.quit()
    else:
      msg.setText('Error')
      msg.exec_()

class RegistrationForm(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle('RegistrationForm')
    self.resize(500,120)

    reg.QGridLayout()

    label_name = QLabel('<font size = "4"> Username </font>')
    self.LineEdit_username = QLineEdit()
    self.LineEdit_username.setPlaceholderText('Enter your username:')
    reg.addWidget(label_name, 0, 0)
    reg.addWidget(self.LineEdit_username, 0, 1)

    self.setLayout(reg)




      

if __name__ == '__main__':
  app = QApplication(sys.argv)

  form = LoginForm()
  form.show()

  sys.exit(app.exec_())


Thank you in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question