在PyQt4中如何启动窗口

时间:2016-07-27 02:56:58

标签: python user-interface pyqt4

您好我是一名正在尝试使用GUI的学生,我正在使用python制作一个简单的登录表单,因为我做了基本布局,我遇到了一个错误,我无法解决变量。这是代码:

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

对不起,如果对齐关闭,我不习惯使用堆栈溢出,但代码会产生以下输出:

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

我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

尝试在__init__课程中设置Ui_Dialog功能:

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

然后,在您的main()函数

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