Problems using method in subclass dialog window to affect main window - python

I'm trying to create a GUI program using PyQT5. I'm new to programming and Python so if I'm explaining things in the wrong way please correct me.
I have a main window that will contain multiple QLineEdit widgets and corresponding "Clear" buttons to clear the user entered data. The main window also contains "Edit" buttons to dispay specific dialog boxes where the data can also be edited. My example has a "User ID" QLineEdit widget/text box, "Clear" and "Edit" push buttons.
The dialog box that appears when "Edit' is clicked has it's own "Clear" button. If the Clear button in the dialog window is clicked, both the QLineEdit widget in the dialog and main window should be cleared.
Problem: When I inherit the main window class from the dialog class the method, clearUserID(), used to clear the User ID field fails to be invoked.
When I do not inherit from the main window class the clearUserID method works and I am able to clear the dialog QLineEdit (UserIDWin_UserID_lnedt) but not the corresponding widget on the main window (UserID_lnedt). All of the code I've tried to clear the main window QLineEdit widget using the dialog "Clear" button caused my program to crash.
Would somebody please help me to better understand the logic behind these principles and how to get my code working? Thank you.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(820, 611)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 801, 551))
self.tabWidget.setObjectName("tabWidget")
self.MainTab = QtWidgets.QWidget()
self.MainTab.setObjectName("MainTab")
self.UserID_Edit_pb = QtWidgets.QPushButton(self.MainTab)
self.UserID_Edit_pb.setGeometry(QtCore.QRect(210, 10, 31, 23))
self.UserID_Edit_pb.setObjectName("UserID_Edit_pb")
self.UserID_Edit_pb.clicked.connect(self.openUserIDWin)
self.UserID_Clear_pb_2 = QtWidgets.QPushButton(self.MainTab)
self.UserID_Clear_pb_2.setGeometry(QtCore.QRect(170, 9, 41, 23))
self.UserID_Clear_pb_2.setObjectName("UserID_Clear_pb_2")
self.UserID_le = QtWidgets.QLineEdit(self.MainTab)
self.label = QtWidgets.QLabel(self.MainTab)
self.label.setGeometry(QtCore.QRect(10, 10, 47, 13))
self.UserID_le.setGeometry(QtCore.QRect(50, 10, 113, 20))
self.UserID_le.setObjectName("UserID_le")
self.tabWidget.addTab(self.MainTab, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 820, 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.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.UserID_Clear_pb_2.clicked.connect(self.UserID_le.clear)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.UserID_Edit_pb.setText(_translate("MainWindow", "Edit"))
self.UserID_Clear_pb_2.setText(_translate("MainWindow", "Clear"))
self.label.setText(_translate("MainWindow", "User ID"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.MainTab), _translate("MainWindow", "Tab"))
def openUserIDWin(self):
UserID_value = self.UserID_le.text()
UserIDWin = QtWidgets.QDialog()
ui = Ui_UserIDWin(UserID_value)
ui.setupUi(UserIDWin)
UserIDWin.exec_();
class Ui_UserIDWin(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, userID):
print("The User ID is:" + userID)
self.userID = userID
def setupUi(self, UserIDWin):
UserIDWin.setObjectName("UserIDWin")
UserIDWin.resize(400, 124)
self.UserIDWin_UserID_lnedt = QtWidgets.QLineEdit(UserIDWin)
self.UserIDWin_UserID_lnedt.setText(self.userID)
self.UserIDWin_UserID_lnedt.setGeometry(QtCore.QRect(20, 50, 113, 20))
self.UserIDWin_UserID_lnedt.setObjectName("UserIDWin_UserID_lnedt")
self.UserIDWin_UserID_lbl = QtWidgets.QLabel(UserIDWin)
self.UserIDWin_UserID_lbl.setGeometry(QtCore.QRect(20, 30, 47, 13))
self.UserIDWin_UserID_lbl.setObjectName("UserIDWin_UserID_lbl")
self.UserIDWin_UserIDClear_pushb = QtWidgets.QPushButton(UserIDWin)
self.UserIDWin_UserIDClear_pushb.setGeometry(QtCore.QRect(140, 50, 41, 23))
self.UserIDWin_UserIDClear_pushb.setObjectName("UserIDWin_UserIDClear_pushb")
self.UserIDWin_Cancel_pushb = QtWidgets.QPushButton(UserIDWin)
self.UserIDWin_Cancel_pushb.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.UserIDWin_Cancel_pushb.setObjectName("UserIDWin_Cancel_pushb")
self.UserIDWin_Next_pushb = QtWidgets.QPushButton(UserIDWin)
self.UserIDWin_Next_pushb.setGeometry(QtCore.QRect(190, 80, 75, 23))
self.UserIDWin_Next_pushb.setObjectName("UserIDWin_Next_pushb")
self.retranslateUi(UserIDWin)
QtCore.QMetaObject.connectSlotsByName(UserIDWin)
#If I do not inherit from "QtWidgets.QMainWindow, Ui_MainWindow" the code below works and invokes clearUserId(). However, I then am having problems using SetText on the main window UserId_le text box and the program crashes.
self.UserIDWin_UserIDClear_pushb.clicked.connect(self.clearUserID)
def retranslateUi(self, UserIDWin):
_translate = QtCore.QCoreApplication.translate
UserIDWin.setWindowTitle(_translate("UserIDWin", "Dialog"))
self.UserIDWin_UserID_lbl.setText(_translate("UserIDWin", "User ID"))
self.UserIDWin_UserIDClear_pushb.setText(_translate("UserIDWin", "Clear"))
self.UserIDWin_Cancel_pushb.setText(_translate("UserIDWin", "Cancel"))
self.UserIDWin_Next_pushb.setText(_translate("UserIDWin", "Next"))
def clearUserID(self):
self.UserIDWin_UserID_lnedt.setText('')
# The line below crashes my program if I am able to invoke this method.
#self.Ui_MainWindow.UserID_le.setText('')
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_())

It seems that they have certain clear concepts about inheritance and good practices but others do not like the following:
PyQt recommends not modifying the code generated by Qt Designer because you probably want to modify the GUI in the future and when using pyuic the initial code is overwritten. Another problem is that beginners do not understand that the class generated by Qt Designer is not a widget but an interface that serves to fill another widget, and consequently can not overwrite the methods of the widgets in addition to other problems.
You only have to modify the object of a class from the object of another class if both have the same scope, in your case when wanting to clean the QLineEdit of the main window from the other window is a dangerous task, instead you should do that logic where both windows have the same scope and that is in the openUserIDWin method.
QLineEdit already has the clear() method that allows to clean, it fulfills the same function as setText("") but the first method is more readable.
Considering the above, the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(820, 611)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 801, 551))
self.tabWidget.setObjectName("tabWidget")
self.MainTab = QtWidgets.QWidget()
self.MainTab.setObjectName("MainTab")
self.UserID_Edit_pb = QtWidgets.QPushButton(self.MainTab)
self.UserID_Edit_pb.setGeometry(QtCore.QRect(210, 10, 31, 23))
self.UserID_Edit_pb.setObjectName("UserID_Edit_pb")
self.UserID_Clear_pb_2 = QtWidgets.QPushButton(self.MainTab)
self.UserID_Clear_pb_2.setGeometry(QtCore.QRect(170, 9, 41, 23))
self.UserID_Clear_pb_2.setObjectName("UserID_Clear_pb_2")
self.UserID_le = QtWidgets.QLineEdit(self.MainTab)
self.label = QtWidgets.QLabel(self.MainTab)
self.label.setGeometry(QtCore.QRect(10, 10, 47, 13))
self.UserID_le.setGeometry(QtCore.QRect(50, 10, 113, 20))
self.UserID_le.setObjectName("UserID_le")
self.tabWidget.addTab(self.MainTab, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 820, 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.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.UserID_Edit_pb.setText(_translate("MainWindow", "Edit"))
self.UserID_Clear_pb_2.setText(_translate("MainWindow", "Clear"))
self.label.setText(_translate("MainWindow", "User ID"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.MainTab), _translate("MainWindow", "Tab"))
class Ui_UserIDWin(object):
def setupUi(self, UserIDWin):
UserIDWin.setObjectName("UserIDWin")
UserIDWin.resize(400, 124)
self.UserIDWin_UserID_lnedt = QtWidgets.QLineEdit(UserIDWin)
self.UserIDWin_UserID_lnedt.setGeometry(QtCore.QRect(20, 50, 113, 20))
self.UserIDWin_UserID_lnedt.setObjectName("UserIDWin_UserID_lnedt")
self.UserIDWin_UserID_lbl = QtWidgets.QLabel(UserIDWin)
self.UserIDWin_UserID_lbl.setGeometry(QtCore.QRect(20, 30, 47, 13))
self.UserIDWin_UserID_lbl.setObjectName("UserIDWin_UserID_lbl")
self.UserIDWin_UserIDClear_pushb = QtWidgets.QPushButton(UserIDWin)
self.UserIDWin_UserIDClear_pushb.setGeometry(QtCore.QRect(140, 50, 41, 23))
self.UserIDWin_UserIDClear_pushb.setObjectName("UserIDWin_UserIDClear_pushb")
self.UserIDWin_Cancel_pushb = QtWidgets.QPushButton(UserIDWin)
self.UserIDWin_Cancel_pushb.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.UserIDWin_Cancel_pushb.setObjectName("UserIDWin_Cancel_pushb")
self.UserIDWin_Next_pushb = QtWidgets.QPushButton(UserIDWin)
self.UserIDWin_Next_pushb.setGeometry(QtCore.QRect(190, 80, 75, 23))
self.UserIDWin_Next_pushb.setObjectName("UserIDWin_Next_pushb")
self.retranslateUi(UserIDWin)
QtCore.QMetaObject.connectSlotsByName(UserIDWin)
def retranslateUi(self, UserIDWin):
_translate = QtCore.QCoreApplication.translate
UserIDWin.setWindowTitle(_translate("UserIDWin", "Dialog"))
self.UserIDWin_UserID_lbl.setText(_translate("UserIDWin", "User ID"))
self.UserIDWin_UserIDClear_pushb.setText(_translate("UserIDWin", "Clear"))
self.UserIDWin_Cancel_pushb.setText(_translate("UserIDWin", "Cancel"))
self.UserIDWin_Next_pushb.setText(_translate("UserIDWin", "Next"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.UserID_Edit_pb.clicked.connect(self.openUserIDWin)
self.UserID_Clear_pb_2.clicked.connect(self.UserID_le.clear)
def openUserIDWin(self):
UserID_value = self.UserID_le.text()
w = UserIDWin(UserID_value)
w.UserIDWin_UserIDClear_pushb.clicked.connect(self.UserID_le.clear)
w.exec_()
class UserIDWin(QtWidgets.QDialog, Ui_UserIDWin):
def __init__(self, userID, parent=None):
super(UserIDWin, self).__init__(parent)
self.setupUi(self)
self.userID = userID
self.UserIDWin_UserID_lnedt.setText(self.userID)
self.UserIDWin_UserIDClear_pushb.clicked.connect(self.UserIDWin_UserID_lnedt.clear)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I recommend you read:
http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
QtDesigner changes will be lost after redesign User Interface

Related

my GUI screen is freeze and having issue for passing the value from 2nd window to 1st window (Pyqt5)

Below are the 2 window codes, 1st is main window and 2nd is widget window:
from Open import Ui_Form
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def open(self):
self.window = QtWidgets.QWidget()
self.ui = Ui_Form()
self.ui.setupUi(self.window)
self.window.show()
def get_result(self):
answer = []
Unsqueeze_result = set(answer)
final = list(Unsqueeze_result)
final.sort()
self.final_list = [x.upper() for x in final]
self.listView.addItems(self.final_list)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listView = QtWidgets.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(270, 140, 256, 371))
self.listView.setObjectName("listView")
self.result_button = QtWidgets.QPushButton(self.centralwidget)
self.result_button.setGeometry(QtCore.QRect(350, 70, 91, 41))
self.result_button.setObjectName("result_button")
self.result_button.pressed.connect(self.get_result)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionOpen.triggered.connect(lambda:self.open())
self.menuFile.addAction(self.actionOpen)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.result_button.setText(_translate("MainWindow", "Result"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
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_())
Below are 2nd window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def __init__(self):
self.list_result = []
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.checkBox_1 = QtWidgets.QCheckBox(Form)
self.checkBox_1.setGeometry(QtCore.QRect(60, 70, 121, 31))
self.checkBox_1.setObjectName("checkBox_1")
self.checkBox_2 = QtWidgets.QCheckBox(Form)
self.checkBox_2.setGeometry(QtCore.QRect(60, 110, 111, 31))
self.checkBox_2.setObjectName("checkBox_2")
self.pushButton_ok = QtWidgets.QPushButton(Form)
self.pushButton_ok.setGeometry(QtCore.QRect(90, 200, 71, 31))
self.pushButton_ok.setObjectName("pushButton")
self.pushButton_ok.pressed.connect(self.OK_thread)
self.pushButton_cancel = QtWidgets.QPushButton(Form)
self.pushButton_cancel.setGeometry(QtCore.QRect(210, 200, 71, 31))
self.pushButton_cancel.setObjectName("pushButton_2")
self.pushButton_cancel.clicked.connect(Form.close)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox_1.setText(_translate("Form", "Test1"))
self.checkBox_2.setText(_translate("Form", "Test 2"))
self.pushButton.setText(_translate("Form", "OK"))
self.pushButton_2.setText(_translate("Form", "Cancel"))
def OK_thread(self):
global submit_execute_thread
self.pushButton_ok.setEnabled(False)
self.pushButton_cancel.setEnabled(False)
submit_execute_thread = QThread()
submit_execute_thread.started.connect(self.ok_execute)
submit_execute_thread.start()
self.check_execute_thread()
def check_execute_thread(self):
if submit_execute_thread.isRunning() == True:
time.sleep(10)
self.check_execute_thread()
else:
Form.close()
def ok_execute(self):
if self.checkBox_1.isChecked() and self.checkBox_2.ischecked():
self.list_result = ["a","b","c","d","e"]
i have 2 issue here:
My objective is click the File --> Open and 2nd window appear, when i tick the checkbox1 and checkbox2 and press ok_button, i connect the ok_button to OK_thread and make a Qthread to ok_execute function to prevent freezing while running the function and keep checking the thread every 10 seconds until it run finish and close the Form window. I am not sure what is the issue here as it could not work, my GUI still freezing after i press OK_button and never run finish.
passing value:
i try to define the list = [] in def init() in both main window and 2nd window but some variable become not declarable such as "Form", so when i open the Form window from main window, it could not work. and another issue is how do i pass the value from Form window which is from ok_execute return self.list_result to Mainwindow, so when i click the result button in Main Window, it will show the list_result in the listview widget.
Please help me modify the code and appreciate your help.
Thank you.
Regards,
cstung89

Push Button in second window does not work

I am new to PyQt5 and have a simple code where one window opens another. The second window contains pushbutton, which after pressing should print a message "push button clicked". The pushbutton works when the second window is called separately and not from the mainwindow. However, when the second window is called by the mainwindow, the pushbutton does nothing.
Code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(110, 30, 211, 111))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(140, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.print_method)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "This is another Window"))
self.pushButton.setText(_translate("Form", "PushButton"))
def print_method(self):
print("push button clicked")
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(300, 190, 191, 91))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.show_new_window)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Another Window"))
def show_new_window(self):
self.Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(self.Form)
self.Form.show()
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_())
It doesn't work because the ui object has no reference, so it gets deleted as soon as show_new_window returns. The result is that since the print_method function is a member of Ui_Form, it doesn't get called because that instance has been deleted.
A possibility could be to make the ui an attribute of the QWidget:
def show_new_window(self):
self.Form = QtWidgets.QWidget()
self.Form.ui = Ui_Form()
self.Form.ui.setupUi(self.Form)
self.Form.show()
In any case, be aware that manually editing files generated by pyuic (or trying to mimic their behavior) is considered bad practice and highly discouraged, as it might lead to confusion and unexpected behavior. Read more about this topic on the official guidelines about using Designer.

How to pass QWidget parameter into a class

I am learning PyQt5, I am trying to create a message box , but its keep giving me error
Traceback (most recent call last):
File "addition_code_main.py", line 47, in clickMethod
QMessageBox.about(self, "Title", "Message")
TypeError: about(QWidget, str, str): argument 1 has unexpected type 'MainWindow_EXEC'
The code I have is is bellow, I am not understanding what i am doing wrong, where to pass this Qwidget parameter,
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QMessageBox
from addition_code import Ui_MainWindow
class MainWindow_EXEC():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(MainWindow)
self.connect_singals()
MainWindow.show()
sys.exit(app.exec_())
def connect_singals(self):
self.ui.pushButton_3.clicked.connect(self.clickMethod)
def clickMethod(self):
QMessageBox.about(self, "Title", "Message")
if __name__ == "__main__":
MainWindow_EXEC()
addition_code.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'addition_code.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# 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.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(150, 40, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(430, 40, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(290, 40, 113, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 100, 81, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(400, 110, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(250, 190, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Addtion"))
self.pushButton_2.setText(_translate("MainWindow", "MysqlConnect"))
self.pushButton_3.setText(_translate("MainWindow", "MessagesBox"))
To understand the problem, just check the documentation:
void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
Displays a simple about box with title title and text text. The about
box's parent is parent.
about() looks for a suitable icon in four locations:
It prefers parent->icon() if that exists.
If not, it tries the top-level widget containing parent.
If that fails, it tries the active window.
As a last resort it uses the Information icon.
The about box
has a single button labelled "OK". On macOS, the about box is popped
up as a modeless window; on other platforms, it is currently
application modal.
As you can see the first parameter must be a QWidget or None but you are passing it a MainWindow_EXEC instance that does not meet that condition by throwing that error.
Considering the above there are several possible solutions:
Pass None as the first parameter.
def clickMethod(self):
QMessageBox.about(None, "Title", "Message")
Move to MainWindow so you have to become a class member.
class MainWindow_EXEC:
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.connect_singals()
self.MainWindow.show()
sys.exit(app.exec_())
def connect_singals(self):
self.ui.pushButton_3.clicked.connect(self.clickMethod)
def clickMethod(self):
QMessageBox.about(self.MainWindow, "Title", "Message")

PyQt5 Designer with Custom Slots

I'm still trying to figure out PyQt and running into another issue that I've been beating my head over for the last several hours. When I use pyuic5 to convert the .ui file into the .py file, part of the output (in the class Ui_MainWindow) connects signals to slots:
self.browseButton.clicked.connect(MainWindow.browseSlot)
self.importButton.clicked.connect(MainWindow.importSlot)
self.lineEdit.returnPressed.connect(MainWindow.returnPressedSlot)
This is called from the main function:
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = MainWindowUI()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Above this, in the same file, I have the following code:
class MainWindowUI(Ui_MainWindow):
def __init__(self):
super().__init__()
self.model = Model()
def setupUi(self, mainWindow):
super().setupUi(mainWindow)
def debugPrint(self, msg):
self.textEdit.append(msg)
def refreshAll(self):
self.lineEdit.setText(self.model.getFileName())
self.textEdit.setText(self.model.getFileContents())
def returnPressedSlot(self):
self.debugPrint('Return key pressed')
def importSlot(self):
self.debugPrint('Import button pressed')
def browseSlot(self):
self.debugPrint('Browse button pressed')
The exact error I'm getting is this:
AttributeError: 'QMainWindow' object has no attribute 'browseSlot'
This actually makes perfect sense, because there's no reason QtWidgets.QMainWindow() should know anything about the custom slots I defined in the MainWindowUI class. So it makes sense that it doesn't work, but I'm confused about what I should be doing differently. This is how every tutorial I've seen sets it up, so clearly I have a fundamental misunderstanding somewhere. Any help in clarifying this issue would be appreciated!
Many thanks in advance.
Edited to add the full code of my Ui_MainWindow class:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# 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(798, 593)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.verticalLayout = QtWidgets.QVBoxLayout(self.tab)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.frame_2 = QtWidgets.QFrame(self.tab)
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.debugTextBrowser = QtWidgets.QTextBrowser(self.frame_2)
self.debugTextBrowser.setGeometry(QtCore.QRect(10, 10, 351, 461))
self.debugTextBrowser.setObjectName("debugTextBrowser")
self.horizontalLayout_2.addWidget(self.frame_2)
self.frame = QtWidgets.QFrame(self.tab)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.browseButton = QtWidgets.QPushButton(self.frame)
self.browseButton.setGeometry(QtCore.QRect(80, 30, 51, 20))
self.browseButton.setObjectName("browseButton")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(80, 10, 281, 20))
self.lineEdit.setObjectName("lineEdit")
self.importButton = QtWidgets.QPushButton(self.frame)
self.importButton.setGeometry(QtCore.QRect(310, 30, 51, 20))
self.importButton.setObjectName("importButton")
self.textEdit = QtWidgets.QTextEdit(self.frame)
self.textEdit.setGeometry(QtCore.QRect(80, 130, 281, 81))
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_2.addWidget(self.frame)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.setupProgressBar = QtWidgets.QProgressBar(self.tab)
self.setupProgressBar.setProperty("value", 0)
self.setupProgressBar.setTextVisible(False)
self.setupProgressBar.setInvertedAppearance(False)
self.setupProgressBar.setTextDirection(QtWidgets.QProgressBar.TopToBottom)
self.setupProgressBar.setObjectName("setupProgressBar")
self.verticalLayout.addWidget(self.setupProgressBar)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.tabWidget.addTab(self.tab_3, "")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.tabWidget.addTab(self.tab_4, "")
self.tab_5 = QtWidgets.QWidget()
self.tab_5.setObjectName("tab_5")
self.tabWidget.addTab(self.tab_5, "")
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 798, 18))
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.tabWidget.setCurrentIndex(0)
self.browseButton.clicked.connect(MainWindow.browseSlot)
self.importButton.clicked.connect(MainWindow.importSlot)
self.lineEdit.returnPressed.connect(MainWindow.returnPressedSlot)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.browseButton.setText(_translate("MainWindow", "Browse"))
self.importButton.setText(_translate("MainWindow", "Import"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Setup"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Production Forecast"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Production Forecast"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_4), _translate("MainWindow", "Page"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_5), _translate("MainWindow", "Page"))
PyQt recommends in your docs that you should inherit from the appropriate widget, in this case QMainWindow, and use Ui_MainWindow as an interface, it is also recommended that you use the decorator #QtCore.pyqtSlot() since you save resources and avoid having problems with overloaded signals.
class MainWindowUI(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.model = Model()
def setupUi(self, mainWindow):
super().setupUi(mainWindow)
def debugPrint(self, msg):
self.textEdit.append(msg)
def refreshAll(self):
self.lineEdit.setText(self.model.getFileName())
self.textEdit.setText(self.model.getFileContents())
#QtCore.pyqtSlot()
def returnPressedSlot(self):
self.debugPrint('Return key pressed')
#QtCore.pyqtSlot()
def importSlot(self):
self.debugPrint('Import button pressed')
#QtCore.pyqtSlot()
def browseSlot(self):
self.debugPrint('Browse button pressed')
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindowUI()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__': main()
The PyQt 5 Designer generates files with .ui extension. The file contains xml code which can be converted to Python code with the pyuic5 command. The pyuic5 command produces a file with .py extension.
But each time the pyuic5 command is run, it overwrites any changes made to the Python file. To prevent this we need to copy the "main" code in the generated Python file to a separate file and then use this file as our main program file. In this file we can import the Python class generated by the pyuic5 command. We can also add our custom slots in this file
This practice of separating Qt Desginer code from custom code is good Qt programming practice. It has been suggested in Section 3 of the video ebook: "Python GUI Programming Recipes using PyQt5".

How do I make PyQt buttons work?

"Create a program in PyQt that is aimed towards benefiting a local community library. The program should be able to add, edit and delete book entries to and from the inventory list.
Include one list widget, one line edit box, one label, four push buttons."
I can drag and drop all these things into PyQt5, save it as .ui, convert to .py and open in IDLE, but how do I make the buttons do anything? Also, how do I get my IDLE to display this box?
This is what the program should look like
This is the code I am left with after saving the .ui file and converting to .py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'listoper.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# 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, 450)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(260, 180, 256, 192))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 230, 171, 91))
self.lineEdit.setObjectName("lineEdit")
self.Enterbookdetails = QtWidgets.QLabel(self.centralwidget)
self.Enterbookdetails.setGeometry(QtCore.QRect(20, 200, 101, 16))
self.Enterbookdetails.setObjectName("Enterbookdetails")
self.Add = QtWidgets.QPushButton(self.centralwidget)
self.Add.setGeometry(QtCore.QRect(10, 330, 75, 23))
self.Add.setObjectName("Add")
self.Delete = QtWidgets.QPushButton(self.centralwidget)
self.Delete.setGeometry(QtCore.QRect(110, 330, 75, 23))
self.Delete.setObjectName("Delete")
self.Edit = QtWidgets.QPushButton(self.centralwidget)
self.Edit.setGeometry(QtCore.QRect(10, 370, 75, 23))
self.Edit.setObjectName("Edit")
self.Deleteall = QtWidgets.QPushButton(self.centralwidget)
self.Deleteall.setGeometry(QtCore.QRect(110, 370, 75, 23))
self.Deleteall.setObjectName("Deleteall")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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", "MainWindow"))
self.Enterbookdetails.setText(_translate("MainWindow", "Enter Book Details:"))
self.Add.setText(_translate("MainWindow", "Add"))
self.Delete.setText(_translate("MainWindow", "Delete"))
self.Edit.setText(_translate("MainWindow", "Edit"))
self.Deleteall.setText(_translate("MainWindow", "Delete All"))
Apparently I am supposed to use the following code, but my buttons still don't work:
The kilogram to pound button:
def btn_KtoP_clicked(self):
kilo = float(self.edt_Kilo.text())
pound = (kilo * 2.2)
self.edt_Pounds.setText(str(pound))
The pound to kilogram button:
def btn_PtoK_clicked(self):
pound = float(self.edt_Pounds.text())
kilo = (pound/2.2)
self.edt_Kilo.setText(str(kilo))
Based on your above code, this should work
import sys
from PyQt5 import QtCore, QtWidgets
from YOUR_UI_MODULE import Ui_MainWindow
class sow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.main_ui=Ui_MainWindow()
self.main_ui.setupUi(self)
self.main_ui.Add.clicked.connect(self.doAdd)
self.main_ui.Edit.clicked.connect(self.doEdit)
def doAdd(self):
print("do add")
def doEdit(self):
print("do edit")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = sow()
myapp.show()
sys.exit(app.exec_())
checkout the examples which came with your PyQT5 installation and start from there.
mine is located here C:\Python34\Lib\site-packages\PyQt5\examples
PyQT has a reference doc, mine is located here C:/Python32/Lib/site-packages/PyQt4/doc/html/classes.html
I will recommend reading this tutorial before coding further, so you know what you are doing.
http://zetcode.com/gui/pyqt5/firstprograms/

Categories