PyQt5: Find which QPushButton is clicked - python

I have multiple buttons & want to know which button is clicked. I have found out the error and know that the sender() function has to work with QWidget rather than the class object, but I can't figure out the solution.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.sender() # Error Ui_MainWindow has no attribute sender
print(sender_button)
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_())

With Qt 4.8 and python2.7, i use partial to pass multi arguments to signal.
from functools import partial
...
def initGui(self):
...
self.btn1.clicked.connect(partial(self.btnListener, "btn1"))
self.btn2.clicked.connect(partial(self.btnListener, "btn2"))
...
def btnListener(self, button_name):
print('button_name {}'.format(button_name))
...
With this method, you can know which button is clicked.
I hope that help you to find the same in QT5.

You need to inherit Ui_MainWindow from MainWindow for this to work.
class Ui_MainWindow(MainWindow):
...

below code worked well.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class DlgMain(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Yeh lay")
self.resize(400,400)
self.btn1 = QPushButton("btn1", self)
self.btn1.move(200,200)
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QPushButton("btn2", self)
self.btn2.move(200,230)
self.btn2.clicked.connect(self.btnListener)
def btnListener(self):
rbt = self.sender()
print(rbt.text())
if __name__ =='__main__':
app = QApplication(sys.argv)
dlgMain = DlgMain()
dlgMain.show()
sys.exit(app.exec_())

here's a way:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUI()
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self..sender()
print(sender_button.text())
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_())

Done this based on #MrLeeh suggestion & it has worked
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.MainWindow.sender()
print(sender_button.text())
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_())

Related

QStackedWidget Window Communication

I Have a main.py file and two QMainWindow.py files as well. When a PushButton is clicked on the second QMainWindow.py file it should open a first QStackedWidget, and on the first QStackedWidget when a PushButton is clicked it should show a second QStackedWidget window.
main.py
from PyQt5 import QtWidgets, QtCore
from mainmenu import MainWindow
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
mainMenu.py
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Reg_Emp_Button.clicked.connect(self.manage_reg_Employees)
self.ui.Report_Button.clicked.connect(self.Employee_Report)
def manage_reg_Employees(self):
self.hide()
self.employeeWindow = EmployeeWindow(self)
self.employeeWindow.show()
def Employee_Report(self):
self.hide()
self.chartWindow = ChartWindow(self)
self.chartWindow.show()
ManageEmployee.py
class EmployeeWindow(QtWidgets.QMainWindow):
def __init__(self, mainMenu):
super(EmployeeWindow, self).__init__()
self.mainMenu = mainMenu
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Back_Button.clicked.connect(self.Back_To_MainMenu)
self.ui.Add_Employee_Button_2.clicked.connect(self.Add_New_Employee)
def Back_To_MainMenu(self):
self.hide()
self.mainMenu.show()

'PySide2.QtWidgets.QMainWindow' object has no attribute 'setSizeGripEnabled'

using Pyside2 and command
pyside2-uic "untitled.ui" -o "gui.py"
I faced such exception
AttributeError: 'PySide2.QtWidgets.QMainWindow' object has no
attribute 'setSizeGripEnabled'
and did not find a solution in internet. Flag -x doesn't help. It answeres:
uic: Unknown option 'x'.
My main.py:
import sys
from PySide2 import QtWidgets
from PySide2.QtCore import QFile
from gui import Ui_Dialog
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_Dialog()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
gui.py:
class Ui_Dialog(object):
def setupUi(self, Dialog):
if not Dialog.objectName():
Dialog.setObjectName(u"Dialog")
Dialog.resize(511, 400)
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
...
Dialog.setSizeGripEnabled(True)
self.verticalLayoutWidget = QWidget(Dialog)
...
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Currency Converter", None))
self.comboBox.setCurrentText("")
...
# retranslateUi
Your code w = QtWidgets.QMainWindow() is not correct
try change your code to w = QtWidgets.QDialog()
I had the same error, but I solved it with this code.
from PySide2.QtWidgets import QApplication, QDialog
from resources.gui import Ui_notes
class Window(QDialog, Ui_notes):
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())`
My window object in QT Designer is called "Dialog" and in 'gui.py' made from .ui file I have such line:
Dialog.setSizeGripEnabled(True)
that cuses a problem.
But if we just comment this line - UI interface starts to work!

Python Pyqt5 QDateEdit Get Date String

I'm trying to build a date printer using Pyqt5 QDateEdit. I can popup the calendar, but I want to write the clicked date's string in the console (or in a label in window). I tried print(self.calendarWidget().document().toPlainText()) or print(self.calendarWidget().currentText()) but it didn't work.
I use this code;
from PyQt5 import QtCore, QtWidgets
class DateEdit(QtWidgets.QDateEdit):
popupSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(DateEdit, self).__init__(parent)
self.setCalendarPopup(True)
self.calendarWidget().installEventFilter(self)
def eventFilter(self, obj, event):
if self.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(DateEdit, self).eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = DateEdit()
w.popupSignal.connect(lambda: print("popup"))
w.show()
sys.exit(app.exec_())
What is its syntax? I didn't find enough documentation for it. Can you help please?
EDIT: The answer
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.dateEdit = QDateEdit(self)
self.lbl = QLabel()
self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
self.dateEdit.setCalendarPopup(True)
layout = QGridLayout()
layout.addWidget(self.dateEdit)
layout.addWidget(self.lbl)
self.setLayout(layout)
self.dateEdit.dateChanged.connect(self.onDateChanged)
def onDateChanged(self, qDate):
print('{0}/{1}/{2}'.format(qDate.day(), qDate.month(), qDate.year()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())

QCalendarWidget Renders Small

I am trying to use the QCalendarWidget but it doesn't render in the user interface as expected. The examples that I have seen show a calendar picker like object, but in my case I get a quite small rendering of a field. Here's what it looks like in the UI:
This is my first time using it so I am not sure if I am missing a step. Any thoughts on what I could be doing incorrectly? Here is the complete code being used:
from PyQt5.QtWidgets import QMainWindow, QCalendarWidget, QLabel
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QtCore.QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(20, 200)
self.setGeometry(100,100,300,300)
self.setWindowTitle('Calendar')
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
Use a layout, for example a QVBoxLayout, in the centralWidget of QMainWindow:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QtWidgets.QCalendarWidget(gridVisible=True)
cal.clicked.connect(self.showDate)
self.lbl = QtWidgets.QLabel()
date = cal.selectedDate()
self.lbl.setText(date.toString())
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(cal)
lay.addWidget(self.lbl)
self.setGeometry(100, 100, 300, 300)
self.setWindowTitle("Calendar")
#QtCore.pyqtSlot(QtCore.QDate)
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

SEGV_MAPERR for PyQt5 object

Why does the following demo code produce a SEGV_MAPERR? How would one fix it?
Hint: Once one deletes the line annotated with "# Ref1", no error is produced. This is not easily done in the production code the problem is abstracted from.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebChannel
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setHtml("")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
page = self.webView.page() # REF1
for i in range(2):
self.init_page()
def init_page(self):
class EditObject(QtCore.QObject):
#QtCore.pyqtSlot(str)
def edit(self, s):
print("test")
editObject = EditObject(self.webView.page())
poChannel = self.webView.page().webChannel()
print(1)
if poChannel is None:
poChannel = QtWebChannel.QWebChannel()
self.webView.page().setWebChannel(poChannel)
print(2)
objects = poChannel.registeredObjects()
print(objects)
poChannel.registerObject("editObject", editObject)
self.webView.setHtml("")
from PyQt5 import QtWebEngineWidgets
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_())
This is similar to Issues with PyQt5==5.10 on Ubuntu 18, but with example code. For the example code, https://gist.github.com/gioditalia/03c9fd5d793aeccbe065fea45d842431 is adapted.
The problem is that poChannel is a local variable that will be deleted after executing init_page, so in the second iteration the poChannel reference will point to an incorrect memory address. So the solution is to extend its cycle to that of the view, so we take advantage of the Qt property and pass it as a parent to self.webView.
poChannel = QtWebChannel.QWebChannel(self.webView)
Although as PyQt points out in the docs and the generated file: # WARNING! All changes made in this file will be lost!, it is not convenient to modify the class generated by the .ui, instead you must create another class that inherits from the appropriate widget and use the interface provided by Qt Designer.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
class EditObject(QtCore.QObject):
#QtCore.pyqtSlot(str)
def edit(self, s):
print("test")
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
for i in range(2):
self.init_page()
def init_page(self):
editObject = EditObject(self.webView.page())
poChannel = self.webView.page().webChannel()
if poChannel is None:
poChannel = QtWebChannel.QWebChannel(self)
self.webView.page().setWebChannel(poChannel)
objects = poChannel.registeredObjects()
poChannel.registerObject("editObject", editObject)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Categories