教学网站:http://zetcode.com/gui/pyqt5/eventssignals/
问题解答:https://stackoverflow.com/questions/9330055/argument-1-has-unexpected-type-ui-mainwindow
https://stackoverflow.com/questions/19775487/calling-a-class-to-build-a-qtreewidget-within-a-window-argument-1-has-unexpecte
Error:argument 1 has unexpected type 'Ui_MainWindow'"
- 将ui类Ui_MainWindow的对象ui作为类uiMain的属性attribute,
初始化后使用ui.***语句设定连接
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
from Ui_login import Ui_MainWindow
class UiMain(QMainWindow):
def __init__(self):
super().__init__()
ui = Ui_MainWindow()
ui.setupUi(self)
ui.btnLogin.clicked.connect(self.btnLoginClicked)
def btnLoginClicked(self):
print('login button clicked')
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = UiMain()
ui.show()
sys.exit(app.exec_())
Alternatively you can store ui as instance attribute:
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
Instead, you need to create a module containing a main window class that does something like this:
from ui import Ui_MainWindow
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
ui = Ui_MainWindow()
ui.setupUi(self)
# other setup code goes here
I would also suggest that you modify the MakeWidget class to something like this:
class MakeWidget(QtGui.QTreeWidget):
def __init__(self, xml, parent=None):
QtGui.QTreeWidget.__init__(self, parent)
so that you can create an instance of it in MainWindow.init like this:
self.treeServiceSelection = MakeWidget(xml, self)
or possibly factor the xml parsing code out into a method that takes the xml as an argument.
网友评论