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
Then I tried to add it into the code developed by Qt designer,
But it shows an error
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
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
and made changes to Qtdesigner code
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
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() |
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_()) |
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 didn't understand most things I read there and after searching a lot I changed to this
1 |
class Ui_MainWindow(QtWidgets.QMainWindow): |
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() |
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_()) |
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