我有一个构建的PyQt5应用程序,看起来就像这样(我知道我有很多导入,我正在学习,所以我现在想要完全自由):
import sys
from PyQt5.QtGui import *
from PyQt5.QWidgets import *
from PyQt5.QtCore import *
class Menu(QMainWindow):
def __init__(self)
super().__init__()
#create bar
bar = self.menuBar()
#create bar menus
file = bar.addMenu("File")
about = bar.addMenu("About")
#create actions
quit_action = QAction("&Quit", self)
quit_action.setShortcut('Ctrl+Q')
about_action = QAction("&About...", self)
#add actions
file.addAction(quit_action)
about.addAction(about_action)
#what to do with actions
quit_action.triggered.connect(self.quit_func)
about_action.triggered.connect(self.about_func)
#window properties
self.setWindowTitle("Hello World")
self.resize(600, 400)
self.show()
def quit_func(self):
sys.exit()
def about_func(self):
pass
class About(QWidget):
def __init__(self):
super().__init__(parent)
#widgets
self.l1 = QLabel('Hello World')
self.l1.setAlignment(Qt.AlignCenter)
self.l2 = QLabel('Description of the Application')
self.l2.setAlignment(Qt.AlignCenter)
#horiz box
h_box = QHBoxLayout()
h_box.addStretch()
h_box.addWidget(self.l2)
h_box.addStretch()
#vert box
v_box = QVBoxLayout()
v_box.addWidget(self.l1)
v_box.addLayout(h_box)
v_box.addStretch()
self.setLayout(v_box)
#window properties
self.setWindowTitle("About Hello World")
self.setFixedSize(250,150)
self.show()
if not QApplication.instance()
app = QApplication(sys.argv)
else:
app = QApplication.instance()
main = Menu()
main.show()
sys.exit(app.exec())
我希望about_func()函数调用About()类,这样我就可以打开一个独立于Menu()类创建的主窗口的窗口。
下面的代码抛出错误:
TypeError: QMainWindow(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'sip.wrappertype'
引用第9行中的super().__init__()
。
我怎样才能以一种工作的方式实现它呢?请随意批评我代码的任何方面。
(已编辑以澄清问题)
发布于 2018-09-18 08:10:36
从你的代码中不能很清楚你是在使用Python 2还是Python 3,不管怎么说,super的基本语法是:
super(yourClass, instance).method(args)
因此,在您的情况下,它们都是错误的:-)第一个应该是:
class Menu(QMainWindow):
def __init__(self, parent=None):
super(Menu, self).__init__(parent)
此外,从Python3中可以省略super()的参数,因此第二个示例可以是:
class About(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
请仔细阅读Built-in Functions。我知道这是一个很长的页面,但它包含了Python的一些基础知识,学习/理解它们几乎是必须的。
https://stackoverflow.com/questions/52377073
复制相似问题