【发布时间】:2019-07-11 03:17:45
【问题描述】:
嗨 :) 请有人帮助我。我必须做一个任务,在 Qtdesigner 的对话框中添加一个 LCDNumber 小部件,将 ui 文件转换为 py,然后创建一个单独的脚本来导入代码以调用 UI 设计和显示,它还必须包含一个计时器来保持更新液晶显示器。 这是我得到的错误
Traceback (most recent call last):
File "C:\PythonPrograms\showtime.pyw", line 4, in <module>
class MyForm(QtGui.QDialog):
AttributeError: 'module' object has no attribute 'QDialog'
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
self.lcdNumber.setGeometry(QtCore.QRect(70, 20, 241, 151))
self.lcdNumber.setObjectName("lcdNumber")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
接下来是 showtime.pyw 文件,假设从 disptime.py 导入代码来调用 UI 设计和显示
import sys
from disptime import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.__init__(self, parent)
self.ui =Ui_Dialog()
self.ui.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm')
self.ui.lcdNumber.display(text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
【问题讨论】:
-
您似乎正在将 PyQt4 与 PyQt5 的示例结合起来,在 PyQt4 中,子模块 QtGui 包含小部件和其他内容,但在 PyQt5 中,小部件在名为 QtWidgets 的其他模块中分离,以便更好地组织,所以 PyQt5 中的 QDialog 属于子模块 QtWidgets ,所以使用 QtWidgets.QDialog 代替 QtGui.QDialog ,我推荐你阅读:pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html