如何修复PyQt5中模块的属性错误

2024-10-04 01:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨:)有人能帮我吗。我必须在Qtdesigner的对话框中添加一个LCDNumber小部件,将ui文件转换为py,然后创建一个单独的脚本来导入代码来调用ui设计和显示,它还必须包含一个计时器来不断更新LCD显示。 这是我得到的错误

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'

^{pr2}$

下一个是文件showtime.pyw假设从中导入代码显示时间.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_())

Tags: 文件代码pyselfuisysclassmodule
1条回答
网友
1楼 · 发布于 2024-10-04 01:26:16

试试看:

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_())

enter image description here

相关问题 更多 >