嗨:)请有人可以帮助我。我必须做一个分配,将一个LCDNumber小部件添加到Qtdesigner中的对话框,将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_())
答案 0 :(得分:0)
尝试一下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets # +++
from disptime import Ui_Dialog # * <-> Ui_Dialog
#class MyForm(QtGui.QDialog): # ---
# def __init__(self, parent=None): # +++
# QtGui.__init__(self, parent) # ---
class MyForm(QtWidgets.QDialog): # +++
def __init__(self): # +++
super().__init__() # +++
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_())