i have two .ui files that converted to python using pyuic5.
first converted ui file is (LoginWindowUI.py) as below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LoginWindow(object):
def setupUi(self, LoginWindow):
LoginWindow.setObjectName("LoginWindow")
LoginWindow.setWindowModality(QtCore.Qt.NonModal)
LoginWindow.setEnabled(True)
LoginWindow.resize(446, 182)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(LoginWindow.sizePolicy().hasHeightForWidth())
LoginWindow.setSizePolicy(sizePolicy)
LoginWindow.setMouseTracking(False)
LoginWindow.setLayoutDirection(QtCore.Qt.RightToLeft)
LoginWindow.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
LoginWindow.setDocumentMode(False)
self.centralwidget = QtWidgets.QWidget(LoginWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(194, 30, 187, 77))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.password_ldt = QtWidgets.QLineEdit(self.layoutWidget)
self.password_ldt.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
self.password_ldt.setText("")
self.password_ldt.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_ldt.setCursorPosition(0)
self.password_ldt.setAlignment(QtCore.Qt.AlignCenter)
self.password_ldt.setObjectName("password_ldt")
self.gridLayout.addWidget(self.password_ldt, 1, 1, 1, 1)
self.password_lbl = QtWidgets.QLabel(self.layoutWidget)
self.password_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.password_lbl.setObjectName("password_lbl")
self.gridLayout.addWidget(self.password_lbl, 1, 0, 1, 1)
self.username_lbl = QtWidgets.QLabel(self.layoutWidget)
self.username_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.username_lbl.setObjectName("username_lbl")
self.gridLayout.addWidget(self.username_lbl, 0, 0, 1, 1)
self.username_ldt = QtWidgets.QLineEdit(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.username_ldt.sizePolicy().hasHeightForWidth())
self.username_ldt.setSizePolicy(sizePolicy)
self.username_ldt.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
self.username_ldt.setText("")
self.username_ldt.setEchoMode(QtWidgets.QLineEdit.Normal)
self.username_ldt.setAlignment(QtCore.Qt.AlignCenter)
self.username_ldt.setObjectName("username_ldt")
self.gridLayout.addWidget(self.username_ldt, 0, 1, 1, 1)
self.login_btn = QtWidgets.QPushButton(self.layoutWidget)
self.login_btn.setCursor(QtGui.QCursor(QtCore.Qt.ClosedHandCursor))
self.login_btn.setAutoDefault(False)
self.login_btn.setDefault(False)
self.login_btn.setObjectName("login_btn")
self.gridLayout.addWidget(self.login_btn, 2, 1, 1, 1)
self.description_lbl = QtWidgets.QLabel(self.centralwidget)
self.description_lbl.setGeometry(QtCore.QRect(30, 40, 141, 41))
self.description_lbl.setText("")
self.description_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.description_lbl.setWordWrap(True)
self.description_lbl.setObjectName("description_lbl")
LoginWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(LoginWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 446, 21))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
LoginWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(LoginWindow)
self.statusbar.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.statusbar.setObjectName("statusbar")
LoginWindow.setStatusBar(self.statusbar)
self.action_cra = QtWidgets.QAction(LoginWindow)
self.action_cra.setObjectName("action_cra")
self.action_db = QtWidgets.QAction(LoginWindow)
self.action_db.setObjectName("action_db")
self.action_exit = QtWidgets.QAction(LoginWindow)
self.action_exit.setObjectName("action_exit")
self.menu.addAction(self.action_cra)
self.menu.addAction(self.action_db)
self.menu.addSeparator()
self.menu.addAction(self.action_exit)
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(LoginWindow)
self.action_exit.triggered.connect(LoginWindow.close)
QtCore.QMetaObject.connectSlotsByName(LoginWindow)
LoginWindow.setTabOrder(self.username_ldt, self.password_ldt)
LoginWindow.setTabOrder(self.password_ldt, self.login_btn)
def retranslateUi(self, LoginWindow):
_translate = QtCore.QCoreApplication.translate
self.password_ldt.setToolTip(_translate("LoginWindow", "رمز عبور خود را وارد کنید."))
self.password_lbl.setText(_translate("LoginWindow", " رمز عبور "))
self.username_lbl.setText(_translate("LoginWindow", "نام کاربری"))
self.username_ldt.setToolTip(_translate("LoginWindow", "لطفاً نام کاربری خود را وارد نمایید."))
self.login_btn.setText(_translate("LoginWindow", "ورود"))
self.menu.setTitle(_translate("LoginWindow", "درباره ما"))
self.action_cra.setText(_translate("LoginWindow", "A"))
self.action_db.setText(_translate("LoginWindow", "B"))
self.action_exit.setText(_translate("LoginWindow", "خروج"))
self.action_exit.setStatusTip(_translate("LoginWindow", "خروج"))
self.action_exit.setShortcut(_translate("LoginWindow", "Ctrl+Q"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LoginWindow = QtWidgets.QMainWindow()
ui = Ui_LoginWindow()
ui.setupUi(LoginWindow)
LoginWindow.show()
sys.exit(app.exec_())
and the second converted .ui file is (MainWindowUI.py) as below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(452, 432)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.cralogo_lbl = QtWidgets.QLabel(self.centralwidget)
self.cralogo_lbl.setGeometry(QtCore.QRect(90, 30, 171, 161))
self.cralogo_lbl.setText("")
self.cralogo_lbl.setPixmap(QtGui.QPixmap("../../.designer/backup/ICONS/CraLogo.png"))
self.cralogo_lbl.setScaledContents(True)
self.cralogo_lbl.setObjectName("cralogo_lbl")
self.exit_btn = QtWidgets.QPushButton(self.centralwidget)
self.exit_btn.setGeometry(QtCore.QRect(140, 340, 125, 40))
self.exit_btn.setMinimumSize(QtCore.QSize(125, 40))
self.exit_btn.setLayoutDirection(QtCore.Qt.LeftToRight)
self.exit_btn.setCheckable(False)
self.exit_btn.setChecked(False)
self.exit_btn.setObjectName("exit_btn")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(20, 10, 389, 152))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.mobile_btn = QtWidgets.QPushButton(self.layoutWidget)
self.mobile_btn.setEnabled(False)
self.mobile_btn.setMinimumSize(QtCore.QSize(125, 40))
self.mobile_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.mobile_btn.setCheckable(False)
self.mobile_btn.setChecked(False)
self.mobile_btn.setObjectName("mobile_btn")
self.gridLayout.addWidget(self.mobile_btn, 1, 0, 1, 1)
self.dataentry_btn = QtWidgets.QPushButton(self.layoutWidget)
self.dataentry_btn.setMinimumSize(QtCore.QSize(125, 40))
self.dataentry_btn.setLayoutDirection(QtCore.Qt.LeftToRight)
self.dataentry_btn.setCheckable(False)
self.dataentry_btn.setChecked(False)
self.dataentry_btn.setObjectName("dataentry_btn")
self.gridLayout.addWidget(self.dataentry_btn, 0, 2, 1, 1)
self.village_btn = QtWidgets.QPushButton(self.layoutWidget)
self.village_btn.setEnabled(False)
self.village_btn.setMinimumSize(QtCore.QSize(125, 40))
self.village_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.village_btn.setCheckable(False)
self.village_btn.setChecked(False)
self.village_btn.setObjectName("village_btn")
self.gridLayout.addWidget(self.village_btn, 1, 1, 1, 1)
self.fix_btn = QtWidgets.QPushButton(self.layoutWidget)
self.fix_btn.setEnabled(False)
self.fix_btn.setMinimumSize(QtCore.QSize(125, 40))
self.fix_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.fix_btn.setCheckable(False)
self.fix_btn.setChecked(False)
self.fix_btn.setObjectName("fix_btn")
self.gridLayout.addWidget(self.fix_btn, 0, 0, 1, 1)
self.post_btn = QtWidgets.QPushButton(self.layoutWidget)
self.post_btn.setEnabled(False)
self.post_btn.setMinimumSize(QtCore.QSize(125, 40))
self.post_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.post_btn.setCheckable(False)
self.post_btn.setChecked(False)
self.post_btn.setObjectName("post_btn")
self.gridLayout.addWidget(self.post_btn, 2, 0, 1, 1)
self.city_btn = QtWidgets.QPushButton(self.layoutWidget)
self.city_btn.setEnabled(False)
self.city_btn.setMinimumSize(QtCore.QSize(125, 40))
self.city_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.city_btn.setCheckable(False)
self.city_btn.setChecked(False)
self.city_btn.setObjectName("city_btn")
self.gridLayout.addWidget(self.city_btn, 0, 1, 1, 1)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(20, 190, 389, 134))
self.layoutWidget1.setObjectName("layoutWidget1")
self.gridLayout_2 = QtWidgets.QGridLayout(self.layoutWidget1)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.rep4_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep4_btn.setEnabled(False)
self.rep4_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep4_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep4_btn.setCheckable(False)
self.rep4_btn.setObjectName("rep4_btn")
self.gridLayout_2.addWidget(self.rep4_btn, 0, 0, 1, 1)
self.rep1_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep1_btn.setEnabled(False)
self.rep1_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep1_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep1_btn.setCheckable(False)
self.rep1_btn.setObjectName("rep1_btn")
self.gridLayout_2.addWidget(self.rep1_btn, 0, 1, 1, 1)
self.report_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.report_btn.setMinimumSize(QtCore.QSize(125, 40))
self.report_btn.setMaximumSize(QtCore.QSize(125, 40))
self.report_btn.setCheckable(False)
self.report_btn.setObjectName("report_btn")
self.gridLayout_2.addWidget(self.report_btn, 0, 2, 1, 1)
self.rep5_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep5_btn.setEnabled(False)
self.rep5_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep5_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep5_btn.setCheckable(False)
self.rep5_btn.setObjectName("rep5_btn")
self.gridLayout_2.addWidget(self.rep5_btn, 1, 0, 1, 1)
self.rep2_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep2_btn.setEnabled(False)
self.rep2_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep2_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep2_btn.setCheckable(False)
self.rep2_btn.setObjectName("rep2_btn")
self.gridLayout_2.addWidget(self.rep2_btn, 1, 1, 1, 1)
self.rep6_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep6_btn.setEnabled(False)
self.rep6_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep6_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep6_btn.setCheckable(False)
self.rep6_btn.setObjectName("rep6_btn")
self.gridLayout_2.addWidget(self.rep6_btn, 2, 0, 1, 1)
self.rep3_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep3_btn.setEnabled(False)
self.rep3_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep3_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep3_btn.setCheckable(False)
self.rep3_btn.setObjectName("rep3_btn")
self.gridLayout_2.addWidget(self.rep3_btn, 2, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 452, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.exit_btn.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.cralogo_lbl.setStatusTip(_translate("MainWindow", "www.cra.ir"))
self.exit_btn.setText(_translate("MainWindow", "خروج"))
self.mobile_btn.setText(_translate("MainWindow", "4"))
self.dataentry_btn.setText(_translate("MainWindow", "A"))
self.village_btn.setText(_translate("MainWindow", "2"))
self.fix_btn.setText(_translate("MainWindow", "3"))
self.post_btn.setText(_translate("MainWindow", "5"))
self.city_btn.setText(_translate("MainWindow", "1"))
self.rep4_btn.setText(_translate("MainWindow", "4"))
self.rep1_btn.setText(_translate("MainWindow", "1"))
self.report_btn.setText(_translate("MainWindow", "B"))
self.rep5_btn.setText(_translate("MainWindow", "5"))
self.rep2_btn.setText(_translate("MainWindow", "2"))
self.rep6_btn.setText(_translate("MainWindow", "6"))
self.rep3_btn.setText(_translate("MainWindow", "3"))
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_())
and my main python file is :
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import mysql.connector
open_main_window_flag = False
class Main_Window():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
self.MWindow = QtWidgets.QMainWindow()
self.ui_MWindow = Ui_MainWindow()
self.ui_MWindow.setupUi(self.MWindow)
self.MWindow.show()
sys.exit(app.exec_())
class login():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
self.LoginWindow = QtWidgets.QMainWindow()
self.ui_login = Ui_LoginWindow()
self.ui_login.setupUi(self.LoginWindow)
self.ui_login.login_btn.clicked.connect(self.loginform)
self.authentication_flag = False
self.LoginWindow.show()
sys.exit(app.exec_())
def loginform(self):
global open_main_window_flag
db_user = self.ui_login.username_ldt.text()
db_pass = self.ui_login.password_ldt.text()
if(db_user == 'admin' and db_pass=='admin'):
print('Welcome')
self.authentication_flag = True
else:
print('Try Again')
self.authentication_flag = False
if self.authentication_flag:
self.LoginWindow.close()
open_main_window_flag = True
if __name__ == '__main__':
mylogin = login()
if open_main_window_flag:
mymainwindow = Main_Window()
I want that when user enter "admin","admin" in username and password this window colsed and open second ui file .
You have the following errors:
There can only be one object of the QApplication, you are creating one for each window and that is incorrect.
Avoid using global variables, they are considered a bad practice and few cases are necessary. For more information read: Why are global variables evil?
Qt Designer does not offer a widget but a class that serves to fill a widget so PyQt recommend to create a suitable widget class and fill it with that class, for more information read: http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
Going to the point, you can not use a flag because the process is asynchronous, in Qt you work with signals, that's the correct way to inform that there is a change, for example a way to keep the classes separate is to create a signal that indicates that you have logged in, close the window and open the other window.
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow):
logged = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(LoginWindow, self).__init__(parent)
self.setupUi(self)
self.login_btn.clicked.connect(self.authenticate)
#QtCore.pyqtSlot()
def authenticate(self):
db_user = self.username_ldt.text()
db_pass = self.password_ldt.text()
if db_user == 'admin' and db_pass=='admin':
self.logged.emit()
self.close()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
login = LoginWindow()
w = Main_Window()
login.logged.connect(w.show)
login.show()
sys.exit(app.exec_())
if __name__ == '__main__': main()
Related
I wrote a program that adds two numbers entered by the user into QLineEdit.
The result of the addition is output to the console in Pycharm.
How to make the result output to the application itself? (so that the user can see the answer).
I guess that there is some special method for this, but what is it?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Probnei(object):
def setupUi(self, Probnei):
Probnei.setObjectName("Probnei")
Probnei.resize(439, 309)
self.centralwidget = QtWidgets.QWidget(Probnei)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 140, 214, 22))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.Second_n = QtWidgets.QLineEdit(self.layoutWidget)
self.Second_n.setObjectName("Second_n")
self.horizontalLayout_2.addWidget(self.Second_n)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 210, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 60, 214, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.First_n = QtWidgets.QLineEdit(self.widget)
self.First_n.setObjectName("First_n")
self.horizontalLayout.addWidget(self.First_n)
Probnei.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Probnei)
self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 21))
self.menubar.setObjectName("menubar")
Probnei.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Probnei)
self.statusbar.setObjectName("statusbar")
Probnei.setStatusBar(self.statusbar)
self.retranslateUi(Probnei)
QtCore.QMetaObject.connectSlotsByName(Probnei)
def retranslateUi(self, Probnei):
_translate = QtCore.QCoreApplication.translate
Probnei.setWindowTitle(_translate("Probnei", "MainWindow"))
self.label_2.setText(_translate("Probnei", "Второе число:"))
self.pushButton.setText(_translate("Probnei", "Посчитать"))
self.label.setText(_translate("Probnei", "Первое число:"))
self.pushButton.clicked.connect(self.cal)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
print(res)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Probnei = QtWidgets.QMainWindow()
ui = Ui_Probnei()
ui.setupUi(Probnei)
Probnei.show()
sys.exit(app.exec_())
How to make the result output to the application itself?
It's not magic. You just have to create a widget that will display the answer. Then set the text on that widget when you do the calculation.
self.answerLabel = QtWidgets.QLabel(self.widget)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
self.answerLabel.setText(res)
I have used Qt designer 5 to create three different windows,
the principal MainWindows.py
the subwindows subVENTANA1.py and subVENTANA2.py
How can I call the sub-windows from MainWindows.py, with the use of mdiArea?
In MainWindows.py only have a menu bar, in this menu there are several subMenus, in this case 2 submenus.
I´ve tried did but I don´t understand how to call with example
like this:
from subVentana import Ui_subwindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent=parent)
subwindow = QMdiSubWindow()
subwindow.setWidget(Ui_subwindow())
self.mdiArea.addSubWindow(subwindow)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(670, 548)
mainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setObjectName("mdiArea")
self.gridLayout.addWidget(self.mdiArea, 0, 0, 1, 1)
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 670, 21))
self.menubar.setObjectName("menubar")
self.menuAn_lisis = QtWidgets.QMenu(self.menubar)
self.menuAn_lisis.setObjectName("menuAn_lisis")
self.menuEliminar_ruido = QtWidgets.QMenu(self.menubar)
self.menuEliminar_ruido.setObjectName("menuEliminar_ruido")
self.menuMenu_C = QtWidgets.QMenu(self.menubar)
self.menuMenu_C.setObjectName("menuMenu_C")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.actionNDVI = QtWidgets.QAction(mainWindow)
self.actionNDVI.setObjectName("actionNDVI")
self.actionEliminar_Ruido = QtWidgets.QAction(mainWindow)
self.actionEliminar_Ruido.setObjectName("actionEliminar_Ruido")
self.actionBrillo_Contraste = QtWidgets.QAction(mainWindow)
self.actionBrillo_Contraste.setObjectName("actionBrillo_Contraste")
self.actionDetecci_n_de_Bordes = QtWidgets.QAction(mainWindow)
self.actionDetecci_n_de_Bordes.setObjectName("actionDetecci_n_de_Bordes")
self.actionEcualizaci_n = QtWidgets.QAction(mainWindow)
self.actionEcualizaci_n.setObjectName("actionEcualizaci_n")
self.actionMostrar_ventna_4 = QtWidgets.QAction(mainWindow)
self.actionMostrar_ventna_4.setObjectName("actionMostrar_ventna_4")
self.actionMostar_subventna2 = QtWidgets.QAction(mainWindow)
self.actionMostar_subventna2.setObjectName("actionMostar_subventna2")
self.menuAn_lisis.addAction(self.actionBrillo_Contraste)
self.menuAn_lisis.addAction(self.actionMostar_subventna2)
self.menuEliminar_ruido.addAction(self.actionEliminar_Ruido)
self.menuEliminar_ruido.addAction(self.actionMostrar_ventna_4)
self.menubar.addAction(self.menuAn_lisis.menuAction())
self.menubar.addAction(self.menuEliminar_ruido.menuAction())
self.menubar.addAction(self.menuMenu_C.menuAction())
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "Venta principal"))
self.menuAn_lisis.setTitle(_translate("mainWindow", "Memu A"))
self.menuEliminar_ruido.setTitle(_translate("mainWindow", "Menu B"))
self.menuMenu_C.setTitle(_translate("mainWindow", "Menu C"))
self.actionNDVI.setText(_translate("mainWindow", "NDVI"))
self.actionEliminar_Ruido.setText(_translate("mainWindow", "Mostar subventna 3"))
self.actionBrillo_Contraste.setText(_translate("mainWindow", "Mostar subventna 1"))
self.actionDetecci_n_de_Bordes.setText(_translate("mainWindow", "Detección de Bordes"))
self.actionEcualizaci_n.setText(_translate("mainWindow", "Ecualización"))
self.actionMostrar_ventna_4.setText(_translate("mainWindow", "Mostar subventna 4"))
self.actionMostar_subventna2.setText(_translate("mainWindow", "Mostar subventna 2"))
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_())
subVENTANA1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subWindow1(object):
def setupUi(self, subWindow1):
subWindow1.setObjectName("subWindow1")
subWindow1.resize(388, 300)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
subWindow1.setFont(font)
subWindow1.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
subWindow1.setLayoutDirection(QtCore.Qt.RightToLeft)
self.centralwidget = QtWidgets.QWidget(subWindow1)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 3)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 0, 3, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 1, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem2, 1, 2, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
subWindow1.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(subWindow1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 388, 21))
self.menubar.setObjectName("menubar")
subWindow1.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(subWindow1)
self.statusbar.setObjectName("statusbar")
subWindow1.setStatusBar(self.statusbar)
self.retranslateUi(subWindow1)
QtCore.QMetaObject.connectSlotsByName(subWindow1)
def retranslateUi(self, subWindow1):
_translate = QtCore.QCoreApplication.translate
subWindow1.setWindowTitle(_translate("subWindow1", "Sub Ventna 1"))
self.groupBox.setTitle(_translate("subWindow1", "Imagen original"))
self.pushButton.setText(_translate("subWindow1", "Abrir imagen"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
subWindow1 = QtWidgets.QMainWindow()
ui = Ui_subWindow1()
ui.setupUi(subWindow1)
subWindow1.show()
sys.exit(app.exec_())
subVENTANA2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subWindow2(object):
def setupUi(self, subWindow2):
subWindow2.setObjectName("subWindow2")
subWindow2.resize(366, 299)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
subWindow2.setFont(font)
subWindow2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
subWindow2.setLayoutDirection(QtCore.Qt.RightToLeft)
self.centralwidget = QtWidgets.QWidget(subWindow2)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 3)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 0, 3, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 1, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem2, 1, 2, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
subWindow2.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(subWindow2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 366, 23))
self.menubar.setObjectName("menubar")
subWindow2.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(subWindow2)
self.statusbar.setObjectName("statusbar")
subWindow2.setStatusBar(self.statusbar)
self.retranslateUi(subWindow2)
QtCore.QMetaObject.connectSlotsByName(subWindow2)
def retranslateUi(self, subWindow2):
_translate = QtCore.QCoreApplication.translate
subWindow2.setWindowTitle(_translate("subWindow2", "Sub Ventana 2"))
self.groupBox.setTitle(_translate("subWindow2", "Imagen procesada"))
self.pushButton.setText(_translate("subWindow2", "Ver"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
subWindow2 = QtWidgets.QMainWindow()
ui = Ui_subWindow2()
ui.setupUi(subWindow2)
subWindow2.show()
sys.exit(app.exec_())
I try to do something like this:
Example
In my case like this: myExample
I hope you understand
setWidget() requires a QWidget argument, not the ui_* subclass used from the pyuic generated files.
from PyQt5 import QtCore, QtGui, QtWidgets
from ui_mainwindow import Ui_mainWindow
from subVENTANA1 import Ui_subWindow1
class MainWindow(QtWidgets.QMainWindow, Ui_mainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
subwindow1 = QtWidgets.QMdiSubWindow()
# create the widget instance
window1 = QtWidgets.QMainWindow()
# create the ui class instance
ui = Ui_subWindow1()
# set the ui for the widget instance
ui.setupUi(window1)
# set the widget for the subwindow
subwindow1.setWidget(window1)
self.mdiArea.addSubWindow(subwindow1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
I want to create a GUI with user inputs on the left side and plots on the right. The Apply button should update the plots which depend on the inputs. Right now the button just clears the TextLabel.
I made a basic Layout with Qt Desginer and tried to implement this example.
Without the plots, focusing the different QLineEdit with the mouse worked, however with the plots focusing only works with the keyboard's tab but not by clicking anymore.
Does the class MyMplCanvas grab the mouse focus or why is it not working ?
Here is the code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import random
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtWidgets
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class MyStaticMplCanvas(MyMplCanvas):
"""Simple canvas with a sine plot."""
def compute_initial_figure(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
self.axes.plot(t, s)
class MyDynamicMplCanvas(MyMplCanvas):
"""A canvas that updates itself every second with a new plot."""
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.update_figure)
timer.start(1000)
def compute_initial_figure(self):
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
def update_figure(self):
# Build a list of 4 random integers between 0 and 10 (both inclusive)
l = [random.randint(0, 10) for i in range(4)]
self.axes.cla()
self.axes.plot([0, 1, 2, 3], l, 'r')
self.draw()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(852, 678)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setContentsMargins(5, 5, 5, 5)
self.formLayout.setObjectName("formLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_3)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setMinimumSize(QtCore.QSize(200, 0))
self.pushButton.setObjectName("pushButton")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.SpanningRole, self.pushButton)
self.horizontalLayout_2.addLayout(self.formLayout)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setContentsMargins(5, 5, 5, 5)
self.gridLayout.setObjectName("gridLayout")
self.widget_2 = QtWidgets.QWidget(self.centralwidget)
self.widget_2.setMinimumSize(QtCore.QSize(300, 300))
self.widget_2.setObjectName("widget_2")
#self.gridLayout.addWidget(self.widget_2, 0, 1, 1, 1)
self.gridLayout.addWidget(MyDynamicMplCanvas(self.widget_2), 0, 1, 1, 1)
self.widget_1 = QtWidgets.QWidget(self.centralwidget)
self.widget_1.setMinimumSize(QtCore.QSize(300, 300))
self.widget_1.setObjectName("widget_1")
#self.gridLayout.addWidget(self.widget_1, 0, 0, 1, 1)
self.gridLayout.addWidget(MyStaticMplCanvas(self.widget_1), 0, 0, 1, 1)
self.widget_4 = QtWidgets.QWidget(self.centralwidget)
self.widget_4.setMinimumSize(QtCore.QSize(300, 300))
self.widget_4.setObjectName("widget_4")
self.gridLayout.addWidget(self.widget_4, 1, 1, 1, 1)
self.widget_3 = QtWidgets.QWidget(self.centralwidget)
self.widget_3.setMinimumSize(QtCore.QSize(300, 300))
self.widget_3.setObjectName("widget_3")
self.gridLayout.addWidget(self.widget_3, 1, 0, 1, 1)
self.horizontalLayout_2.addLayout(self.gridLayout)
self.gridLayout_2.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 852, 20))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuDatei.menuAction())
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.label.clear)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "Apply"))
self.menuDatei.setTitle(_translate("MainWindow", "File"))
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_())
The problem is caused by self.widget_1 and self.widget_2 that are on top of the layout, preventing the mouse events from propagating.
The idea is to replace the self.widget_1 and self.widget_2 with the canvases as shown below:
...
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setContentsMargins(5, 5, 5, 5)
self.gridLayout.setObjectName("gridLayout")
self.widget_2 = MyDynamicMplCanvas(self.centralwidget) # <---
self.widget_2.setMinimumSize(QtCore.QSize(300, 300))
self.widget_2.setObjectName("widget_2")
self.gridLayout.addWidget(self.widget_2, 0, 1, 1, 1)
# self.gridLayout.addWidget(MyDynamicMplCanvas(self.widget_2), 0, 1, 1, 1)
self.widget_1 = MyStaticMplCanvas(self.centralwidget) # <---
self.widget_1.setMinimumSize(QtCore.QSize(300, 300))
self.widget_1.setObjectName("widget_1")
self.gridLayout.addWidget(self.widget_1, 0, 0, 1, 1)
# self.gridLayout.addWidget(MyStaticMplCanvas(self.widget_1), 0, 0, 1, 1)
self.widget_4 = QtWidgets.QWidget(self.centralwidget)
self.widget_4.setMinimumSize(QtCore.QSize(300, 300))
self.widget_4.setObjectName("widget_4")
self.gridLayout.addWidget(self.widget_4, 1, 1, 1, 1)
self.widget_3 = QtWidgets.QWidget(self.centralwidget)
self.widget_3.setMinimumSize(QtCore.QSize(300, 300))
self.widget_3.setObjectName("widget_3")
self.gridLayout.addWidget(self.widget_3, 1, 0, 1, 1)
...
I have this small window code and I want to be able to get the x/y location for the mouse when clicked. I do not see what is the problem in this code, and why it doesn't show me the mouse location
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QGraphicsScene, QMainWindow
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
class Ui_layout_window(QMainWindow):
def setupUi(self, layout_window):
layout_window.setObjectName("layout_window")
layout_window.resize(749, 711)
layout_window.setMouseTracking(True)
self.centralwidget = QtWidgets.QWidget(layout_window)
self.centralwidget.setMouseTracking(True)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setContentsMargins(10, 10, 10, 10)
self.gridLayout.setSpacing(10)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 727, 609))
self.scrollAreaWidgetContents.setMouseTracking(True)
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout_2 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout_2.setContentsMargins(5, 5, 5, 5)
self.gridLayout_2.setSpacing(10)
self.gridLayout_2.setObjectName("gridLayout_2")
self.rot_tgt_btn = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
font = QtGui.QFont()
font.setPointSize(10)
self.rot_tgt_btn.setFont(font)
self.rot_tgt_btn.setObjectName("rot_tgt_btn")
self.gridLayout_2.addWidget(self.rot_tgt_btn, 0, 2, 1, 1)
self.scale_spinBox = QtWidgets.QSpinBox(self.scrollAreaWidgetContents)
self.scale_spinBox.setFont(font)
self.scale_spinBox.setObjectName("scale_spinBox")
self.scale_spinBox.setRange(10, 5000)
self.scale_spinBox.setValue(1000)
self.gridLayout_2.addWidget(self.scale_spinBox, 0, 5, 1, 1)
self.crop_btn = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.crop_btn.setFont(font)
self.crop_btn.setObjectName("crop_btn")
self.gridLayout_2.addWidget(self.crop_btn, 0, 3, 1, 1)
self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView.setObjectName("graphicsView")
self.gridLayout_2.addWidget(self.graphicsView, 1, 0, 1, 6)
self.ror_lft_btn = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.ror_lft_btn.setFont(font)
self.ror_lft_btn.setObjectName("ror_lft_btn")
self.gridLayout_2.addWidget(self.ror_lft_btn, 0, 1, 1, 1)
self.scale_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
font = QtGui.QFont()
font.setPointSize(10)
self.scale_label.setFont(font)
self.scale_label.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.scale_label.setObjectName("scale_label")
self.gridLayout_2.addWidget(self.scale_label, 0, 4, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scene = QGraphicsScene()
self.pixmap = QPixmap("talia.png")
self.pixmap_scl = self.pixmap
self.scene.addPixmap(self.pixmap_scl)
self.graphicsView.setScene(self.scene)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
self.buttonBox = QtWidgets.QDialogButtonBox(self.centralwidget)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)
layout_window.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(layout_window)
self.menubar.setGeometry(QtCore.QRect(0, 0, 749, 23))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuImage = QtWidgets.QMenu(self.menubar)
self.menuImage.setObjectName("menuImage")
layout_window.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(layout_window)
self.statusbar.setObjectName("statusbar")
layout_window.setStatusBar(self.statusbar)
self.actionLoad = QtWidgets.QAction(layout_window)
self.actionLoad.setObjectName("actionLoad")
self.actionCrop = QtWidgets.QAction(layout_window)
self.actionCrop.setObjectName("actionCrop")
self.actionZoom = QtWidgets.QAction(layout_window)
self.actionZoom.setObjectName("actionZoom")
self.actionRotate = QtWidgets.QAction(layout_window)
self.actionRotate.setObjectName("actionRotate")
self.actionRotate_Right = QtWidgets.QAction(layout_window)
self.actionRotate_Right.setObjectName("actionRotate_Right")
self.actionRotate_Left = QtWidgets.QAction(layout_window)
self.actionRotate_Left.setObjectName("actionRotate_Left")
self.actionCrop_2 = QtWidgets.QAction(layout_window)
self.actionCrop_2.setObjectName("actionCrop_2")
self.menuFile.addAction(self.actionLoad)
self.menuFile.addAction(self.actionCrop)
self.menuFile.addAction(self.actionZoom)
self.menuFile.addAction(self.actionRotate)
self.menuImage.addAction(self.actionRotate_Right)
self.menuImage.addAction(self.actionRotate_Left)
self.menuImage.addAction(self.actionCrop_2)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuImage.menuAction())
self.statusbar.showMessage("text")
self.retranslateUi(layout_window)
self.buttonBox.accepted.connect(layout_window.close)
self.buttonBox.rejected.connect(layout_window.close)
self.scale_spinBox.editingFinished.connect(self.resizeimg)
QtCore.QMetaObject.connectSlotsByName(layout_window)
def retranslateUi(self, layout_window):
_translate = QtCore.QCoreApplication.translate
layout_window.setWindowTitle(_translate("layout_window", "MainWindow"))
self.rot_tgt_btn.setText(_translate("layout_window", "Rotate Right"))
self.crop_btn.setText(_translate("layout_window", "Crop"))
self.ror_lft_btn.setText(_translate("layout_window", "Rotate Left"))
self.scale_label.setText(_translate("layout_window", "Scale: "))
self.menuFile.setTitle(_translate("layout_window", "File"))
self.menuImage.setTitle(_translate("layout_window", "Image"))
self.actionLoad.setText(_translate("layout_window", "Load"))
self.actionCrop.setText(_translate("layout_window", "Crop"))
self.actionZoom.setText(_translate("layout_window", "Zoom"))
self.actionRotate.setText(_translate("layout_window", "Rotate"))
self.actionRotate_Right.setText(_translate("layout_window", "Rotate Right"))
self.actionRotate_Left.setText(_translate("layout_window", "Rotate Left"))
self.actionCrop_2.setText(_translate("layout_window", "Crop"))
def resizeimg(self):
zm_fct = self.scale_spinBox.value()
pixmap_scl = self.pixmap.scaled(zm_fct, zm_fct, QtCore.Qt.KeepAspectRatio)
print(zm_fct)
self.scene.clear()
self.scene.addPixmap(pixmap_scl)
self.graphicsView.setScene(self.scene)
def mousePressEvent(self, p):
x = p.x()
y = p.y()
print("On Hover", p.pos().x(), p.pos().y())
def mouseMoveEvent(self, e):
x = e.x()
y = e.y()
print("On Hover", e.pos().x(), e.pos().y())
text = "x: {0}, y: {1}".format(x, y)
self.statusbar.showMessage(text)
def mousePressEvent(self, event):
print(event)
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
layout_window = QtWidgets.QMainWindow()
ui = Ui_layout_window()
ui.setupUi(layout_window)
layout_window.show()
sys.exit(app.exec_())
You are defining the function mousePressEvent() twice, and then you never call or bind it to anything.
I am still working on the GUI for my Database and now i have a different error:
Traceback (most recent call last):
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 84, in <module>
ex = Ui_MainWindow()
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 16, in __init__
self.setupUi(self)
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 55, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'Ui_MainWindow' object has no attribute 'setCentralWidget'
I dont understand a thing about PyQt so i dont understand what the problem is.
The code is:
(This is the edited version, Raw Version is Posted below)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Menu.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(387, 317)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Natan Samuel Geldorp.Remytop-PC/Documents/music-note-sound-256.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.ButtonRemove = QtWidgets.QPushButton(self.centralwidget)
self.ButtonRemove.setObjectName("ButtonRemove")
self.gridLayout.addWidget(self.ButtonRemove, 10, 0, 1, 1)
self.ButtonShowAll = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowAll.setObjectName("ButtonShowAll")
self.gridLayout.addWidget(self.ButtonShowAll, 7, 0, 1, 1)
self.ButtonShowSpecific = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowSpecific.setObjectName("ButtonShowSpecific")
self.gridLayout.addWidget(self.ButtonShowSpecific, 8, 0, 1, 1)
self.Text = QtWidgets.QLabel(self.centralwidget)
self.Text.setObjectName("Text")
self.gridLayout.addWidget(self.Text, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.ButtonAdd = QtWidgets.QPushButton(self.centralwidget)
self.ButtonAdd.setObjectName("ButtonAdd")
self.gridLayout.addWidget(self.ButtonAdd, 9, 0, 1, 1)
self.ButtonExit = QtWidgets.QPushButton(self.centralwidget)
self.ButtonExit.setObjectName("ButtonExit")
self.gridLayout.addWidget(self.ButtonExit, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 387, 21))
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", "Main Menu"))
self.ButtonRemove.setText(_translate("MainWindow", "Remove an album"))
self.ButtonShowAll.setText(_translate("MainWindow", "Show all registerd albums"))
self.ButtonShowSpecific.setText(_translate("MainWindow", "Show a specific album"))
self.Text.setText(_translate("MainWindow", "Welcome to the MusicDatabase Version 2.1-2017"))
self.label.setText(_translate("MainWindow", "Please select one of the options below."))
self.ButtonAdd.setText(_translate("MainWindow", "Add an album"))
self.ButtonExit.setText(_translate("MainWindow", "Exit"))
self.label_2.setText(_translate("MainWindow", "Authors: N.S. Geldorp & H. Nahumury"))
self.ButtonExit.clicked.connect(self.ButtonExit)
def ButtonExit(self):
exit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(app.exec_())
Does anyone see the problem?
-Natan
Posting Raw Convert:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Menu.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# 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(387, 317)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Natan Samuel Geldorp.Remytop-PC/Documents/music-note-sound-256.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.ButtonRemove = QtWidgets.QPushButton(self.centralwidget)
self.ButtonRemove.setObjectName("ButtonRemove")
self.gridLayout.addWidget(self.ButtonRemove, 10, 0, 1, 1)
self.ButtonShowAll = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowAll.setObjectName("ButtonShowAll")
self.gridLayout.addWidget(self.ButtonShowAll, 7, 0, 1, 1)
self.ButtonShowSpecific = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowSpecific.setObjectName("ButtonShowSpecific")
self.gridLayout.addWidget(self.ButtonShowSpecific, 8, 0, 1, 1)
self.Text = QtWidgets.QLabel(self.centralwidget)
self.Text.setObjectName("Text")
self.gridLayout.addWidget(self.Text, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.ButtonAdd = QtWidgets.QPushButton(self.centralwidget)
self.ButtonAdd.setObjectName("ButtonAdd")
self.gridLayout.addWidget(self.ButtonAdd, 9, 0, 1, 1)
self.ButtonExit = QtWidgets.QPushButton(self.centralwidget)
self.ButtonExit.setObjectName("ButtonExit")
self.gridLayout.addWidget(self.ButtonExit, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 387, 21))
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", "Main Menu"))
self.ButtonRemove.setText(_translate("MainWindow", "Remove an album"))
self.ButtonShowAll.setText(_translate("MainWindow", "Show all registerd albums"))
self.ButtonShowSpecific.setText(_translate("MainWindow", "Show a specific album"))
self.Text.setText(_translate("MainWindow", "Welcome to the MusicDatabase Version 2.1-2017"))
self.label.setText(_translate("MainWindow", "Please select one of the options below."))
self.ButtonAdd.setText(_translate("MainWindow", "Add an album"))
self.ButtonExit.setText(_translate("MainWindow", "Exit"))
self.label_2.setText(_translate("MainWindow", "Authors: N.S. Geldorp & H. Nahumury"))
Qt Designer serves to generate the design of the view, pyuic5 converts that design into python code, this element must be used with logic, in addition it is recommended not to modify it.
first solution:
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
Second Solution:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
ui = Ui_MainWindow()
ui.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Third solution:
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())