简体   繁体   中英

Getting widget is not defined error when clicking login button in pyqt5

I am having trouble with making a button to open different .ui file but I am keep running into an issue where it says that 'widget' is not defined and then the program crashes.

Could someone give me what I might have done wrong with my gotoLogin?

Below are my code and ui files.

main.py

import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QWidget
from PyQt5.QtGui import QPixmap

import sqlite3

# loading welcome screen
class welcomeScreen(QDialog):
    def __init__(self):
        super(welcomeScreen, self).__init__()
        loadUi("welcomeScreen.ui", self)
        self.login.clicked.connect(self.gotoLogin)

    def gotoLogin(self):
        login = loginScreen()
        widget.addWidget(login)
        widget.setCurrentIndex(widget.currentIndex()+1)

class loginScreen(QDialog):
    def __init__(self):
        super(loginScreen, self).__init__()
        loadUi("login.ui",self)
        self.passwordField.setEchoMode(QtWidgets.QLineEdit.Password)
        self.login.clicked.connect(self.loginFunction)

    def loginFunction(self):
        user = self.emailField.text()
        password = self.passwordField.text()

        if len(user)==0 or len(password)==0:
            self.loginError.setText("Please input all the fields.")

        else:
            conn = sqlite3.connect("petData.db")
            cur = conn.cursor()
            query = 'SELECT password FROM login_info WHERE username = \''+user+"\'"
            cur.execute(query)
            result_pass = cur.fetchone()[0]
            if (result_pass == password):
                print("Successfully logged in.")
                self.error.setText("")
            else:
                self.loginError.setText("Invalid Username or Password")

# main
def main():
    app = QApplication(sys.argv)
    welcome = welcomeScreen()
    widget = QtWidgets.QStackedWidget()
    widget.addWidget(welcome)
    widget.setFixedWidth(1200)
    widget.setFixedHeight(800)
    widget.show()
    try:
        app.exec_()
        #sys.exit(app_exec())
    except:
        print("Exiting gracefully")

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

welcomeScreen.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1200</width>
    <height>800</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QWidget" name="bgWidget" native="true">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1200</width>
     <height>800</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">QWidget#bgWidget{
background-color:qlineargradient(spread:pad, x1:0.0590406, y1:0.165, x2:1, y2:1, stop:0 rgba(209, 107, 165, 255), stop:1 rgba(255, 255, 255, 255))}</string>
   </property>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>510</x>
      <y>190</y>
      <width>191</width>
      <height>71</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 36pt &quot;MS Shell Dlg 2&quot;; color:rgb(255, 255, 255)
</string>
    </property>
    <property name="text">
     <string>Cat Care</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>440</x>
      <y>230</y>
      <width>311</width>
      <height>111</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;MS Shell Dlg 2&quot;; color:rgb(255, 255, 255)</string>
    </property>
    <property name="text">
     <string>Sign in or create a new account</string>
    </property>
   </widget>
   <widget class="QPushButton" name="login">
    <property name="geometry">
     <rect>
      <x>440</x>
      <y>340</y>
      <width>301</width>
      <height>41</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">border-radius:20px; 
background-color: rgb(170, 255, 255);
font: 14pt &quot;MS Shell Dlg 2&quot;;
</string>
    </property>
    <property name="text">
     <string>Log in</string>
    </property>
   </widget>
   <widget class="QPushButton" name="create">
    <property name="geometry">
     <rect>
      <x>440</x>
      <y>420</y>
      <width>301</width>
      <height>41</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">border-radius:20px; 
background-color: rgb(170, 255, 255);
font: 14pt &quot;MS Shell Dlg 2&quot;;
</string>
    </property>
    <property name="text">
     <string>Create a new account</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

login.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1200</width>
    <height>800</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QWidget" name="bgWidget" native="true">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1200</width>
     <height>800</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">QWidget#bgWidget{
background-color:qlineargradient(spread:pad, x1:0.0590406, y1:0.165, x2:1, y2:1, stop:0 rgba(209, 107, 165, 255), stop:1 rgba(255, 255, 255, 255))}</string>
   </property>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>200</y>
      <width>191</width>
      <height>71</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 36pt &quot;MS Shell Dlg 2&quot;; color:rgb(255, 255, 255)
</string>
    </property>
    <property name="text">
     <string>Login</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>230</y>
      <width>311</width>
      <height>111</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;MS Shell Dlg 2&quot;; color:rgb(255, 255, 255)</string>
    </property>
    <property name="text">
     <string>Sign in to your existing account</string>
    </property>
   </widget>
   <widget class="QPushButton" name="login">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>540</y>
      <width>301</width>
      <height>41</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">border-radius:20px; 
background-color: rgb(170, 255, 255);
font: 14pt &quot;MS Shell Dlg 2&quot;;
</string>
    </property>
    <property name="text">
     <string>Log in</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>510</x>
      <y>130</y>
      <width>191</width>
      <height>71</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 36pt &quot;MS Shell Dlg 2&quot;; color:rgb(255, 255, 255)
</string>
    </property>
    <property name="text">
     <string>Cat Care</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="emailField">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>360</y>
      <width>301</width>
      <height>41</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color:rgba(0,0,0,0);
font: 12pt &quot;MS Shell Dlg 2&quot;;</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="passwordField">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>450</y>
      <width>301</width>
      <height>41</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color:rgba(0,0,0,0);
font: 12pt &quot;MS Shell Dlg 2&quot;;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>330</y>
      <width>281</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 10pt &quot;MS Shell Dlg 2&quot;;</string>
    </property>
    <property name="text">
     <string>Username</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>420</y>
      <width>281</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 10pt &quot;MS Shell Dlg 2&quot;;</string>
    </property>
    <property name="text">
     <string>Password</string>
    </property>
   </widget>
   <widget class="QLabel" name="loginError">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>500</y>
      <width>291</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 12pt &quot;MS Shell Dlg 2&quot;; color:red;</string>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Error

Traceback (most recent call last):
  File "C:\Users\documents\sideProject\pyqt5\CatCare\main.py", line 18, in gotoLogin
    widget.addWidget(login)
NameError: name 'widget' is not defined

In your code widget is a local variable of main . This means that it is only visible within the scope of main . To be able to use it within welcomeScreen you would have to make it a global variable. That being said, it's rarely a good idea to have a class modify global variables. In your case a better solution would be to remove the functionality of adding widgets to the stacked widget from welcomeScreen altogether. Instead, you could subclass QStackedWidget and move all the functionality of adding widgets and switching between widgets in there. The subclass could look something like this:

class MainWindow(QtWidgets.QStackedWidget):
    def __init__(self):
        super().__init__()
        self.welcome_screen = welcomeScreen()
        self.login_screen = loginScreen()

        self.addWidget(self.welcome_screen)
        self.addWidget(self.login_screen)

        self.setFixedWidth(1200)
        self.setFixedHeight(800)

        self.welcome_screen.login.clicked.connect(self.goto_login)
        self.goto_welcome()

    def goto_login(self):
        self.setCurrentIndex(self.indexOf(self.login_screen))

    def goto_welcome(self):
        self.setCurrentIndex(self.indexOf(self.welcome_screen))

welcomeScreen would simply be something like

class welcomeScreen(QDialog):
    def __init__(self):
        super(welcomeScreen, self).__init__()
        loadUi("welcomeScreen.ui", self)

and the main function would become

def main():
    app = QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    try:
        app.exec_()
        #sys.exit(app_exec())
    except:
        print("Exiting gracefully")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM