繁体   English   中英

单击pyqt5中的登录按钮时,获取小部件未定义错误

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

我在制作按钮以打开不同的 .ui 文件时遇到问题,但我一直遇到一个问题,它说“小部件”未定义,然后程序崩溃。

有人可以告诉我我的 gotoLogin 可能做错了什么吗?

下面是我的代码和 ui 文件。

主文件

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()

欢迎屏幕.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>

登录界面

<?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>

错误

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

在您的代码widgetmain的局部变量。 这意味着它仅在main的范围内可见。 为了能够在welcomeScreen使用它,您必须将其设为全局变量。 话虽如此,让一个类修改全局变量很少是一个好主意。 在您的情况下,更好的解决方案是从welcomeScreen完全删除将小部件添加到堆叠小部件的功能。 相反,您可以QStackedWidget并移动添加小部件和在小部件之间切换的所有功能。 子类可能如下所示:

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只是类似于

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

并且主要功能将成为

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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