Pyqt5 名称错误 [英] Pyqt5 NameError
本文介绍了Pyqt5 名称错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找出为什么这会给我一个 NameError....类名 App(QDialog):
是出现错误的那个.我完全按照 youtube 视频关注,虽然他的代码有效,但我的却没有.请帮我解决这个问题.谢谢:)
I am trying to find why this gives me a NameError....
Class name App(QDialog):
is the one that has the error. I was following exactly as youtube video, while his code works, mine doesn't.
Please help me on this. Thanks :)
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_())
推荐答案
NameError: name 'QDialog' is not defined
您收到此错误是因为您忘记导入 QDialog.只需将其添加到您的 QWidgets 导入之一的末尾,例如:
You are getting this error because you forgot to import QDialog. Just add it to the end of one of your QWidgets imports such as:
from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog
另外,你会得到一个属性错误,因为 self.top 被调用,但从未定义.将其添加到 init 函数中:
Also, you are going to get an attribute error because self.top is called, but never defined. Add it in the init function:
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()
这篇关于Pyqt5 名称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文