0

Hi :) please can someone assist me. I have to do an assignment where i add a LCDNumber widget to the dialog in Qtdesigner, convery the ui file to py and then create a separate script to import the code to invoke the UI design and display, it also must include a timer to keep updating the LCD display. This is the Error i get

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

Next is file showtime.pyw which is suppose to import the code from disptime.py to invoke the UI design and display

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • It seems that you are combining examples of PyQt4 with PyQt5, in PyQt4 the submodule QtGui contained the widgets plus other things, but in PyQt5 the widgets were separated in other module called QtWidgets to have a better organization, so QDialog in PyQt5 belongs to the submodule QtWidgets , so use QtWidgets.QDialog instead of QtGui.QDialog, I recommend you read: http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html – eyllanesc Feb 17 '19 at 19:38

1 Answers1

0

Try it:

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

S. Nick
  • 12,879
  • 8
  • 25
  • 33