程序员

pyqt5 连接问题 argument 1 has unexpe

2018-03-06  本文已影响0人  绍重先

教学网站: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'"


#!/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.

上一篇下一篇

猜你喜欢

热点阅读