2017-02-10 78 views
0

我想找到爲什麼這給了我一個NameError .... 類名App(QDialog):是有一個錯誤。我完全遵循youtube視頻,而他的代碼工作,我的不是。 請幫助我。謝謝:)Pyqt5 NameError

import sys 
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QMessageBox, QBoxLayout 
from PyQt5.QtGui import QIcon 
from PyQt5.QtCore import pyqtSlot 
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem 
from PyQt5.QtWidgets import QInputDialog, QLineEdit 


class App(QDialog): 

    def __init__(self): 
     super().__init__() 
     self.title = "PyQt5 example - pythonspot.com" 
     self.left = 10 
     self.right = 10 
     self.width = 640 
     self.height = 400 
     self.initUI() 

    def initUI(self): 
     self.setWindowTitle(self.title) 
     self.setGeometry(self.left, self.top, self.width, self.height) 

     age = self.getAge() 
     print(age) 

     self.show() 

    def getAge(self): 
     age, okPressed = QInputDialog.getInt(self, "Get Integer", "Age:", 18, 16, 130, 1) 
     if okPressed: 
      return age 


if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    ex = App() 
    sys.exit(app.exec_()) 
+0

錯誤消息告訴您爲什麼。 – ekhumoro

+0

我知道..但它不告訴如何解決它... –

回答

1
NameError: name 'QDialog' is not defined 

因爲你忘了導入QDialog的您收到此錯誤。只需將它添加到如您QWidgets進口的一個結束:

from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog 

而且,你會因爲self.top被調用來獲取屬性錯誤,但從來沒有定義。將其添加到初始值功能:

def __init__(self): 
    super().__init__() 
    self.title = "PyQt5 example - pythonspot.com" 
    self.left = 10 
    self.right = 10 
    self.width = 640 
    self.height = 400 
    self.top = 10 
    self.initUI() 
+1

可能'self.right'應該被替換'self.top'。 – ekhumoro

+0

看起來你是對的。接得好。 –