2017-01-11 100 views
1

我写了一个Python 3.4的代码,它使用PyQt4的GUI模块,但是Python PyQt的界面代码当我运行它不会显示任何模块好心帮没有运行

import sys 
from PyQt4 import QtGui,QtCore 

class window(QtGui.QMainWindow): 

    def _init_(self): 
     super(Window, self)._init_() 
     self.setGeometry(50,50,500,300) 
     self.setWindowTitle("Tallman Server") 
     self.setWindowIcon(QtGui.QIcon("tracking.png")) 
     self.home() 
    def home(): 
      btn=QtGui.QPushButton("Quit",self) 
      btn.clicked.connect(QtCore.QCoreApplication.instance().quit) 
      self.show() 


def run():  
     app=QtGui.QApplication(sys.argv) 
     GUI=window() 
     sys.exit(app.exec_()) 

run() 

回答

1

首先,函数的名称为__init__,而不是_init_。 其次,您必须将self参数添加到home()

这些更改将解决您的问题。

修改后的代码:

import sys 
from PyQt4 import QtGui,QtCore 

class window(QtGui.QMainWindow): 

    def __init__(self): 
     super(window, self).__init__() 
     self.setGeometry(50,50,500,300) 
     self.setWindowTitle("Tallman Server") 
     self.setWindowIcon(QtGui.QIcon("tracking.png")) 
     self.home() 
    def home(self): 
     btn=QtGui.QPushButton("Quit",self) 
     btn.clicked.connect(QtCore.QCoreApplication.instance().quit) 
     self.show() 


def run(): 
    app=QtGui.QApplication(sys.argv) 
    GUI=window() 
    sys.exit(app.exec_()) 

run() 
+0

感谢很大的帮助我在PyQt的尼夫新认识的小错误可能使程序无法正常工作。 – tallman

+0

@tallman:'__init__'和'self'是python面向对象特性的一部分,与'pyqt'无关。我建议你让自己熟悉课程,例如[本文档](https://docs.python.org/2/tutorial/classes.html#class-objects)对其进行了解释。否则,你将在未来遇到类似的问题 – hansaplast