Todd Owen
Todd Owen

Reputation: 97

In PyQt4 how do i initiate a window

Hello I am a student that is experimenting with GUI's and I was working in python making a simple log-in form and as I made the base layout I encountered an error where I could not solve the variables. This is the code:

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig)

def main(): 
    app = QtGui.QApplication(sys.argv) 
    w = Ui_Dialog(__init__) 
    w.show() 
    sys.exit(app.exec_()) 

class Ui_Dialog(object):
    def __init__(self, parent=None):
    QtGui.QMainWindow.__init__(self, parent)
    self.addWidgets()
    self.setupUi(self)

def setupUi(self, Dialog):
    Dialog.setObjectName(_fromUtf8("Dialog"))
    Dialog.resize(380, 272)
    Dialog.setMinimumSize(QtCore.QSize(380, 272))
    Dialog.setMaximumSize(QtCore.QSize(380, 272))
    self.buttonBox = QtGui.QDialogButtonBox(Dialog)
    self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
          self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
    self.buttonBox.setCenterButtons(True)
    self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
    self.user_name = QtGui.QTextEdit(Dialog)
    self.user_name.setGeometry(QtCore.QRect(110, 50, 221, 31))
    self.user_name.setAutoFillBackground(False)
    self.user_name.setObjectName(_fromUtf8("user_name"))
    self.user_pass = QtGui.QTextEdit(Dialog)
    self.user_pass.setGeometry(QtCore.QRect(110, 120, 221, 31))
    self.user_pass.setAutoFillBackground(False)
    self.user_pass.setObjectName(_fromUtf8("user_pass"))
    self.label = QtGui.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(20, 60, 81, 21))
    self.label.setObjectName(_fromUtf8("label"))
    self.label_2 = QtGui.QLabel(Dialog)
    self.label_2.setGeometry(QtCore.QRect(20, 120, 81, 21))
    self.label_2.setObjectName(_fromUtf8("label_2"))

    self.retranslateUi(Dialog)
    QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
    QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
    QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
    Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
    self.label.setText(_translate("Dialog", "USER NAME", None))
    self.label_2.setText(_translate("Dialog", "PASSWORD", None))

if __name__ == "__main__":
    main()

sorry if the alignment is off i am not used to having to use stack overflow but the code results in the following output:

Traceback (most recent call last):
  File "C:\Users\todd7\Desktop\PY_auth\design.py", line 75, in <module>
    main()
  File "C:\Users\todd7\Desktop\PY_auth\design.py", line 28, in main
   w = Ui_Dialog(__init__)
NameError: name '__init__' is not defined

and i am not sure how to fix the issue.

Upvotes: 0

Views: 83

Answers (1)

Peter Wang
Peter Wang

Reputation: 1838

Try making your __init__ function in your Ui_Dialog class this:

def __init__(self, window, parent=None):
    self.addWidgets()
    self.setupUi(self)
    window.show()

Then, in your main() function,

def main(): 
    app = QtGui.QApplication(sys.argv) 
    w = Ui_Dialog(QtGui.QMainWindow())
    sys.exit(app.exec_()) 

Upvotes: 1

Related Questions