Python Forum
Qt and QtPy Edit window & argument 1 has unexpected type 'Ui_MainWindow' Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Qt and QtPy Edit window & argument 1 has unexpected type 'Ui_MainWindow' Error
#1
I'm developing an application for my 3D printer. I only have basic knowledge in python(started three weeks ago for this purpose), so I decided using Qt Designer after referring some tutorials. In a tutorial (there he developed GUI using QtPy with out using designer), the tutor adds a button, upon clicking, it will pop-up a dialogue box asking confirmation. I tried to replicate it in my application. first I followed his coding

and here's the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Window(QtWidgets.QMainWindow):
 
        btn.clicked.connect(self.close_application)
        btn.resize(btn.minimumSizeHint())
        btn.move(0, 100)
 
        self.show()
 
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("test")
        self.setWindowIcon(QtGui.QIcon('clay.png'))
 
 
 
        self.home()
 
    def home(self):
 
        #push button
        btn = QtWidgets.QPushButton("Quit", self)
 
    def close_application(self):
 
        #message box
        choice = QtWidgets.QMessageBox.question(self, "extract!", "get inside!!!",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
 
        if choice == QtWidgets.QMessageBox.Yes:
            print("extracting")
            sys.exit()
 
        else:
            pass
 
 
def run():
    app = QtWidgets.QApplication(sys.argv)
 
    GUI = Window()
    sys.exit(app.exec_())
 
run()
Then I tried to add it into the code developed by Qt designer,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(70, 90, 93, 28))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton.clicked.connect(self.close_app)
 
    def close_app(self):
        #message box
        choice = QtWidgets.QMessageBox.question(self, "Exit", "Are you sure?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
 
        if choice == QtWidgets.QMessageBox.Yes:
            print("extracting")
            sys.exit()
 
        else:
            pass
 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
But it shows an error

1
2
3
4
5
Traceback (most recent call last):
 
File "C:\Users\New User\Desktop\test\test - Copy.py", line 40, in close_app
    choice = QtWidgets.QMessageBox.question(self, "Exit", "Are you sure?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'
I asked this question in stackoverflow and one guy told me the first parameter of question() is a widget and directed me to this link

i didn't understand most things I read there and after searching a lot I changed to this
1
class Ui_MainWindow(QtWidgets.QMainWindow):
and it started working. I don't what this change does. Can anyone explain this to me??

Also today I added a Edit window to the code

This from the tutorial I used

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Window(QtWidgets.QMainWindow):
 
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("test")
        self.setWindowIcon(QtGui.QIcon('clay.png'))
 
        #options inside file menu
 
     
        openEditor = QtWidgets.QAction("&Editor", self)
        openEditor.setShortcut("Ctrl+E")
        openEditor.setStatusTip("Open Editor")
        openEditor.triggered.connect(self.editor)
        #status bar
        self.statusBar()
 
        #file menu
        mainMenu = self.menuBar()
         
        editorMenu = mainMenu.addMenu("&Editor")
        editorMenu.addAction(openEditor)
 
 
        self.home()
 
    def home(self):
 
         
        self.show()
 
    
    def editor(self):
        self.textEdit = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit)
 
     
 
def run():
    app = QtWidgets.QApplication(sys.argv)
 
    GUI = Window()
    sys.exit(app.exec_())
 
run()
and made changes to Qtdesigner code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_MainWindow(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(70, 90, 93, 28))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        self.menuEdit = QtWidgets.QMenu(self.menubar)
        self.menuEdit.setObjectName("menuEdit")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionEditor = QtWidgets.QAction(MainWindow)
        self.actionEditor.setObjectName("actionEditor")
        self.menuEdit.addAction(self.actionEditor)
        self.menubar.addAction(self.menuEdit.menuAction())
 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
        self.actionEditor.setText(_translate("MainWindow", "Editor"))
        self.pushButton.clicked.connect(self.close_app)
        self.actionEditor.triggered.connect(self.editor)
 
    def editor(self):
        self.textEdit = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit)
 
    def close_app(self):
        #message box'
        choice = QtWidgets.QMessageBox.question(self, "Exit", "Are you sure?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
 
        if choice == QtWidgets.QMessageBox.Yes:
            print("extracting")
            sys.exit()
 
        else:
            pass
 
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
But when I click Editor nothing opens?
Here also with out QtWidget.QMainWindow in Ui_MainWindow shows error??

This is the tutorial I'm following https://www.youtube.com/watch?v=4Mg6bw1MmAE

This is how my application looks like

[Image: open?id=1XSzWcym1xBCCKsH1PvTi_MivKSWCiFyX]
Reply
#2
1
MainWindow.setCentralWidget(self.centralwidget)
you already set the button as centralwidget
Reply
#3
Thanks for the reply, But you have to explain that to me???? What does that mean?
Reply
#4
There is too much wrong code...

Do you really need Qtdesigner? Better learn without it.

Take a look at my PlainTextEdit or RichTextEdit on github.
Reply
#5
Most code is generated using PyQt and Qt5.
I only added
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
self.pushButton.clicked.connect(self.close_app)
        self.actionEditor.triggered.connect(self.editor)
  
    def editor(self):
        self.textEdit = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit)
  
    def close_app(self):
        #message box'
        choice = QtWidgets.QMessageBox.question(self, "Exit", "Are you sure?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
  
        if choice == QtWidgets.QMessageBox.Yes:
            print("extracting")
            sys.exit()
  
        else:
            pass
this part
Reply
#6
You should definetly use QtDesigner to make the UI. Not only it will look much better, it reduces the noise of your app logic. Here's an example where you can load the .ui file directly (if you don't intend to package the app in the future, you can simply remove the code about 'foo.gui_main'): https://gitlab.com/snippets/1743613

Then inside the Main() class, connect your buttons just like you did
1
self.pushButton.clicked.connect(self.close_app)
Reply
#7
I do not understand your concept.
You place the button somewhere in the window where you want to have the text field later.
Then either the button or the text field is not visible.
Use a toolbar and place the button there.
Or place the button in the statusbar.

Do not use something like "Exit", "Are you sure?"

Users hate that.

better ask for saving changes.

here is a simple example, choose "New" from File Menu to show the Text Field

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QApplication,
                             QTextEdit, QAction, QMenu, QMainWindow)
from PyQt5.QtGui import QIcon, QTextCursor, QKeySequence
from PyQt5.QtCore import Qt
import sys
 
class myEditor(QMainWindow):
    def __init__(self, parent = None):
        super(myEditor, self).__init__(parent)
        self.editor = QTextEdit()
        self.editor.setTabStopWidth(20)
 
        bar=self.menuBar()
        self.filemenu=bar.addMenu("File")
        self.separatorAct = self.filemenu.addSeparator()
        self.filemenu.addAction(QAction(QIcon.fromTheme('document-new'), "New", self, triggered = self.newAct, shortcut = "Ctrl+n"))
        self.filemenu.addAction(QAction(QIcon.fromTheme('document-open'), "Open", self, triggered = self.openAct, shortcut = "Ctrl+o"))
        self.filemenu.addAction(QAction(QIcon.fromTheme('document-save'), "Save", self, triggered = self.saveAct, shortcut = "Ctrl+s"))
        self.filemenu.addAction(QAction(QIcon.fromTheme('document-save-as'), "Save as ...", self, triggered = self.saveAsAct, shortcut = "Shift+Ctrl+s"))
        self.filemenu.addSeparator()
        self.filemenu.addAction(QAction(QIcon.fromTheme('application-exit'), "Exit", self, triggered = self.exitAct, shortcut = "Ctrl+q"))
         
        editmenu = bar.addMenu("Edit")
        editmenu.addAction(QAction(QIcon.fromTheme('edit-undo'), "Undo", self, triggered = self.editor.undo, shortcut = "Ctrl+u"))
        editmenu.addAction(QAction(QIcon.fromTheme('edit-redo'), "Redo", self, triggered = self.editor.redo, shortcut = "Shift+Ctrl+u"))
        editmenu.addSeparator()
        editmenu.addAction(QAction(QIcon.fromTheme('edit-copy'), "Copy", self, triggered = self.editor.copy, shortcut = "Ctrl+c"))
        editmenu.addAction(QAction(QIcon.fromTheme('edit-cut'), "Cut", self, triggered = self.editor.cut, shortcut = "Ctrl+x"))
        editmenu.addAction(QAction(QIcon.fromTheme('edit-paste'), "Paste", self, triggered = self.editor.paste, shortcut = "Ctrl+v"))
        editmenu.addAction(QAction(QIcon.fromTheme('edit-delete'), "Delete", self, triggered = self.editor.cut, shortcut = "Del"))
        editmenu.addSeparator()
        editmenu.addAction(QAction(QIcon.fromTheme('edit-select-all'), "Select All", self, triggered = self.editor.selectAll, shortcut = "Ctrl+a"))
 
        self.layoutV = QVBoxLayout()
#        self.layoutV.addWidget(self.editor)
        mq = QWidget(self)
        mq.setLayout(self.layoutV)
        self.setCentralWidget(mq)
 
        self.createStatusBar()
 
    def createStatusBar(self):
        self.statusBar().showMessage("Ready", 0)
 
    def newAct(self):
        self.layoutV.addWidget(self.editor)
        self.statusBar().showMessage("TextEdit visible", 0)
 
    def openAct(self):
        return
 
    def saveAct(self):
        return
 
    def saveAsAct(self):
        return
 
    def exitAct(self):
        print("Goodbye ...")
        app.quit()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = myEditor()
#    win.setWindowIcon(QIcon.fromTheme("gnome-mime-text-x-python"))
    win.setWindowTitle("Editor")
    win.setMinimumSize(640,250)
    win.showMaximized()
    app.exec_()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 2,569 Mar-17-2024, 09:37 AM
Last Post: deanhystad
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 1,753 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [Kivy] Type error:takes 1 positional argument but 2 required hammer 3 5,063 Nov-09-2021, 06:01 AM
Last Post: deanhystad
  Syntax Error: Positional argument follows keyword argument Rama02 3 6,129 Feb-09-2021, 06:10 PM
Last Post: deanhystad
  [Tkinter] How to make message box error stay on top of window scratchmyhead 1 11,025 May-10-2020, 10:21 PM
Last Post: scratchmyhead
  tkinter window and turtle window error 1885 3 8,260 Nov-02-2019, 12:18 PM
Last Post: 1885
  update a variable in parent window after closing its toplevel window gray 5 11,124 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020