PyQt5 QPushButton fires more than once - python-3.x

is there a way in Python3 PyQt5 to get the number of times a signal is connected to a slot i.e.:
QPushButton.clicked.connect(foo)
to have an idea of how many times the slot (foo) will be called upon emitting the signal ?
I am not talking of counters inside my code but a way to get that number from
where PyQt5 stores that info

Mechanically I would consider placing a
print('any message') #like this you are directly notified via the console console or terminal,...
in each methode you will be calling, for example:
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QPushButton
import sys
#import other modules
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# Geometry of main window:
self.setGeometry(200, 200, 300, 400)
# create a Button Btn1
self.Btn1 = QPushButton('Fire Button :)', self)
self.Btn1.clicked.connect(self.Fire1)
self.Btn1.move(0, 0)
self.Btn2 = QPushButton('2nd Fire_Button :)', self)
self.Btn2.clicked.connect(self.Fire2)
self.Btn2.move(130, 0)
def Fire1(self):
print('Worked, Fired once!...details(...)')
def Fire2(self):
print('It is Working, but he needs to add his code! Thanks')
self.setStyleSheet('background:rgba(80, 97, 15, 184);')
self.resize(600, 210)
pass
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
it's my answer is helping you (it's good!), otherwise provide us with more detailed infomations or sample of code you are working-on in order to better help you...

Related

How to create Signal for QDockWidget?

When QTabWidget is used, it is straightforward to call a function (e.g. on_tab_changed) when any of the tabs is clicked on with something like self.currentChanged.connect(self.on_tab_changed). However, I cannot figure out how to do similarly with QDockWidget. I was able to come up with a simplified MRE that reads:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OOP")
self.uinterface()
self.show()
def uinterface(self):
self.dock = QDockWidget("OOP example", self)
self.listWidget = QListWidget()
self.listWidget.addItem("One")
self.listWidget.addItem("Two")
self.listWidget.addItem("Three")
self.dock.setWidget(self.listWidget)
self.dock.currentChanged.connect(self.on_tab_changed) # This is the line that I'd like to modify
def on_tab_changed(self, index: int) -> None:
print ("DO SOMETHING")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
This doesn't work because currentChanged is not a Signal for QDockWidget (which is for QTabWidget) although both derive from QWidget. I want to come up with a relatively simple way to accomplish this task as converting QDockWidget into QTabWidget is relatively straightforward for the MRE but a lot more complex and time consuming for the whole app.
P.S. Based on some of the comments, maybe the code could have read
...
self.dock.setWidget(self.listWidget)
self.dock.Clicked.connect(self.on_dockwidget_clicked)
def on_dockwidget_clicked(self) -> None:
print ("DO SOMETHING")
albeit this still would not work and it'd be necessary to use the accepted solution.
You could overwrite the the event method a QDockWidget subclass and listen for mousePressEvents and emit a custom signal that sends some kind of identifier, in case you have multiple dockWidgets and you need to know which one sent the signal. Then you can set your mainWindow to listen for the custom signal and connect you method slot to it.
For Example:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class DockWidget(QDockWidget):
hasFocus = pyqtSignal([QDockWidget])
def __init__(self, text, parent=None):
super().__init__(text, parent=parent)
self.setObjectName(text)
def event(self, event):
if event.type() == QEvent.MouseButtonPress and event.button() == 1:
self.hasFocus.emit(self)
return super().event(event)
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OOP")
self.uinterface()
self.show()
def uinterface(self):
self.dock = DockWidget("OOP example", self)
self.listWidget = QListWidget()
self.listWidget.addItem("One")
self.listWidget.addItem("Two")
self.listWidget.addItem("Three")
self.dock.setWidget(self.listWidget)
self.dock.hasFocus.connect(self.on_dock_focus)
def on_dock_focus(self, dockwidget):
print(f"DO SOMETHING WITH {dockwidget.objectName()}")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

Why does the label show only the last set image? (PyQt5) [duplicate]

I have got this problem. I´m trying to set text on a lineEdit object on pyqt4, then wait for a few seconds and changing the text of the same lineEdit. For this I´m using the time.sleep() function given on the python Time module. But my problem is that instead of setting the text, then waiting and finally rewrite the text on the lineEdit, it just waits the time it´s supposed to sleep and only shows the final text. My code is as follows:
from PyQt4 import QtGui
from gui import *
class Ventana(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.button.clicked.connect(self.testSleep)
def testSleep(self):
import time
self.lineEdit.setText('Start')
time.sleep(2)
self.lineEdit.setText('Stop')
def mainLoop(self, app ):
sys.exit( app.exec_())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Ventana()
window.show()
sys.exit(app.exec_())
You can't use time.sleep here because that freezes the GUI thread, so the GUI will be completely frozen during this time.
You should probably use a QTimer and use it's timeout signal to schedule a signal for deferred delivery, or it's singleShot method.
For example (adapted your code to make it run without dependencies):
from PyQt4 import QtGui, QtCore
class Ventana(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setLayout(QtGui.QVBoxLayout())
self.lineEdit = QtGui.QLineEdit(self)
self.button = QtGui.QPushButton('clickme', self)
self.layout().addWidget(self.lineEdit)
self.layout().addWidget(self.button)
self.button.clicked.connect(self.testSleep)
def testSleep(self):
self.lineEdit.setText('Start')
QtCore.QTimer.singleShot(2000, lambda: self.lineEdit.setText('End'))
def mainLoop(self, app ):
sys.exit( app.exec_())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Ventana()
window.show()
sys.exit(app.exec_())
Also, take a look at the QThread sleep() function, it puts the current thread to sleep and allows other threads to run. https://doc.qt.io/qt-5/qthread.html#sleep
You can't use time.sleep here because that freezes the GUI thread, so the GUI will be completely frozen during this time.You can use QtTest module rather than time.sleep().
from PyQt4 import QtTest
QtTest.QTest.qWait(msecs)
So your code should look like:
from PyQt4 import QtGui,QtTest
from gui import *
class Ventana(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.button.clicked.connect(self.testSleep)
def testSleep(self):
import time
self.lineEdit.setText('Start')
QtTest.QTest.qWait(2000)
self.lineEdit.setText('Stop')
def mainLoop(self, app ):
sys.exit( app.exec_())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Ventana()
window.show()
sys.exit(app.exec_())

slot to right click menu action does does not work

I have written the below code to which I finally managed to add menu but connecitn menu to a function doesnt seem to work:
import os
from PyQt5 import uic
from PyQt5 import QtWidgets
from PyQt5 import QtCore
FILE_LOCATION = os.path.dirname(os.path.realpath(__file__))
class MainDialogWindow(QtWidgets.QDialog):
def __init__(self):
super(MainDialogWindow,self).__init__()
ui_file = os.path.join(FILE_LOCATION, "example.ui")
self._ui = uic.loadUi(ui_file, self)
self.registerCallbacks()
self.initUI()
def initUI(self):
"""Initialize the UI.
"""
self.textBrowser.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
def registerCallbacks(self):
self.textBrowser.customContextMenuRequested.connect(self.context_menu)
# self.connect(self.textBrowser, QtCore.Signal('customContextMenuRequested(const QPoint &)'), self.context_menu)
def context_menu(self, pos):
menu = QtWidgets.QMenu(self)
action = menu.addAction("clear")
menu.exec_(self.mapToGlobal(pos))
action.trigered.connect(self.clear)
def clear(self):
"""Slot to claer text.
"""
print("clear")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainDialogWindow()
window.show()
window.setGeometry(500, 300, 300, 300)
sys.exit(app.exec_())
please helpp,, I want call the clear function from the right click menu
I don't seem to understand how the menu.exec_() method works, that method blocks the execution of sequential tasks until the user selects a QAction from the QMenu. In your case, for example, until when you press "clear" and the triggered signal is emitted (note: you have a typo), but at that moment there is no connection, so the clear method will not be called. The solution is to make the connection before invoking the QMenu exec_():
def context_menu(self, pos):
menu = QtWidgets.QMenu(self)
action = menu.addAction("clear")
action.triggered.connect(self.clear)
menu.exec_(self.mapToGlobal(pos))

PyQt5 all button signals/events?

The signal for MOUSE1 on the button is widget.clicked, what are the ones for MOTION and MOUSE2? Also if anyone knows a site with all signals listed it would really help
import sys, pyautogui
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def pressed_mouse2():
pass
def window():
app = QApplication(sys.argv)
root = QMainWindow()
root.setGeometry(200, 200, 500, 500)
root.setWindowTitle('Test')
root.setWindowFlags(QtCore.Qt.FramelessWindowHint)
root.setAttribute(Qt.WA_TranslucentBackground)
button = QtWidgets.QPushButton(root)
#i need here to signal when user has pressed on MOUSE2 on the button
button.clicked.connect(clicked)
button.move(50,50)
root.show()
sys.exit(app.exec_())
window()
You just need to connect the button's clicked signal to your function.
button.clicked.connect(pressed_mouse2)
Now when you click the button you can execute any code here:
def pressed_mouse2():
print('Button clicked')
There are many kinds of widgets, each with different signals. You can find them in the Qt documentation. Here are the signals for QAbstractButton, which is inherited by QPushButton.
There is no predefined signal for a right click on the button, but you can subclass QPushButton and emit your own signal in the mousePressEvent().
class Button(QPushButton):
right_clicked = pyqtSignal()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mousePressEvent(self, event):
super().mousePressEvent(event)
if event.button() == Qt.RightButton:
self.right_clicked.emit()
And it will respond with:
button = Button(root)
button.right_clicked.connect(pressed_mouse2)

Start the script in PyQt5 and python3 [duplicate]

I have created a form using PyQt4 which has a push button. On this push button I want to call another python script which looks like this:
File1.py:
import sys
from PyQt4 import QtCore, QtGui
from file1_ui import Ui_Form
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
File1_ui.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(120, 200, 95, 20))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.close)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form", "Close", None, QtGui.QApplication.UnicodeUTF8))
File2.py
import sys
from PyQt4 import Qt
from taurus.qt.qtgui.application import TaurusApplication
app = TaurusApplication(sys.argv)
panel = Qt.QWidget()
layout = Qt.QHBoxLayout()
panel.setLayout(layout)
from taurus.qt.qtgui.panel import TaurusForm
panel = TaurusForm()
model = [ 'test/i1/1/%s' % p for p in props ]
panel.setModel(model)
panel.show()
sys.exit(app.exec_())
File1_ui.py is created from the Qtdesigner and then I am using File1.py to execute it.So File2.py when executed alone opens up a panel and displays few attributes.I want this script to be called on the button click in the first form(file1.py) which I created using Qtdesigner.Could you let me know how I could achieve this functionality.Thanks.
You will need to make some modifications to File2.py to make the appropriate calls depending on whether it is running standalone or not. When you are launching the script via File1.py there will already be a QApplication instance with event loop running, so trying to create another and run its event loop will cause problems.
Firstly, move the core part of your script into its own function. This will allow you to easily call it from File1.py. You can then handle the case where the script is running standalone and needs to create a QApplication instance and start its event loop. (I am not familiar the the taurus library you are using, but you can probably substitute TaurusApplication for QtGui.QApplication)
File2.py:
import sys
from PyQt4 import QtCore, QtGui
def runscript():
panel = QtGui.QWidget()
layout = QtGui.QHBoxLayout(panel)
return panel # Must return reference or panel will be deleted upon return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
panel = runscript()
panel.show()
sys.exit(app.exec_())
Assuming your files are in the same directory you can simply write import File2 and use File2.runscript() to run your code. You then just need to connect the function to your pushbuttons clicked() signal to run it. The only problem here is that the reference to the QWidget returned from the runscript() function will be lost (and the object deleted) if you connect directly to runscript(). For this reason I created a method launch_script() which saves a reference in MyForm.
File1.py:
import sys
from PyQt4 import QtCore, QtGui
from file1_ui import Ui_Form
import File2
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
# This is a bit of a hack.
self.ui.pushButton.clicked.connect(self.launch_script)
def launch_script(self):
self.panel = File2.runscript()
self.panel.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
I don't use Qt Designer, so I don't know the correct way to go about connecting the signal to launch_script(). The code I have written should work, but obviously violates OOP principles and is dependent on the name of the pushbutton widget assigned by the software.

Resources