簡體   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