如何使用 python 和 pyQt 显示正确的帧
How to show the correct frame with python and pyQt
我是 python 的初学者,我尝试用 pyQt 生成框架。
这是我的代码,我遇到了一些问题,无法显示正确的框架。
一开始我写的,可以显示我想要的结果
#!/usr/bin/python
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle('simple2')
widget.show()
sys.exit(app.exec_())
后来改了面向对象的写法,显示不了我除掉的框架
#!/usr/bin/python
import sys
from PyQt4 import QtGui
class Apple(QtGui.QWidget):
def _int_(self,parent=None):
super().__init__()
self.widget = QtGui.QWidget()
self.resize(250, 150)
self.setWindowTitle('simple2')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.show()
sys.exit(app.exec_())
有人知道我该如何解决我的错误吗?
您的代码有错字
def _int_(self,parent=None):
应该是
def __init__(self, parent=None):
以这种方式编辑您的代码:
#!/usr/bin/python
# -* coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Apple(QtGui.QMainWindow):
def _init_(self): # if you have no parent you don't need to write parent = None. And yes, there was a typo
super(Apple, self).__init__() # it's a little bit better than super().__init__()
#self.widget = QtGui.QWidget() # you've already init QWidget. This code do nothing
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.setWindowTitle('simple2') # here must be window title
mywidget.resize(250, 150) # and resizing
mywidget.show()
sys.exit(app.exec_())
现在你得到你想要的
我是 python 的初学者,我尝试用 pyQt 生成框架。
这是我的代码,我遇到了一些问题,无法显示正确的框架。
一开始我写的,可以显示我想要的结果
#!/usr/bin/python
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle('simple2')
widget.show()
sys.exit(app.exec_())
后来改了面向对象的写法,显示不了我除掉的框架
#!/usr/bin/python
import sys
from PyQt4 import QtGui
class Apple(QtGui.QWidget):
def _int_(self,parent=None):
super().__init__()
self.widget = QtGui.QWidget()
self.resize(250, 150)
self.setWindowTitle('simple2')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.show()
sys.exit(app.exec_())
有人知道我该如何解决我的错误吗?
您的代码有错字
def _int_(self,parent=None):
应该是
def __init__(self, parent=None):
以这种方式编辑您的代码:
#!/usr/bin/python
# -* coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Apple(QtGui.QMainWindow):
def _init_(self): # if you have no parent you don't need to write parent = None. And yes, there was a typo
super(Apple, self).__init__() # it's a little bit better than super().__init__()
#self.widget = QtGui.QWidget() # you've already init QWidget. This code do nothing
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.setWindowTitle('simple2') # here must be window title
mywidget.resize(250, 150) # and resizing
mywidget.show()
sys.exit(app.exec_())
现在你得到你想要的