Home > Article > Backend Development > PyQt4 button click register multiple times when clicked
I am new to pyqt4 and after many searches I found no information about the problem I am seeing in the gui.
The problem is that when the user clicks the getsingleitems button, the function runs the same number of times as the user clicks getallitems. An example is if the user clicks getallitems to populate the items field and then clicks getsingleitem, then getitems runs once and the results are printed once as expected. However, if the user selects another item from the list and clicks getallitems again, and then clicks getsingleitem, the result is that getitem runs 2x, so 2x is printed. Each run will increment, so even without changing the selection, clicking getallitems 4x and then getitem will run 4x by clicking getsingleitem. The only way to refresh it is to close the gui and reopen it. Any help would be greatly appreciated.
class UpdateItem(QDialog, updateitem_ui.Ui_updateitem): def __init__(self): QDialog.__init__(self) self.setupUi(self) tests = ['Test1', 'Test2', 'Test3'] self.list.addItems(tests) self.exit.clicked.connect(self.close) self.setFocus() self.getAllItems.clicked.connect(self.getitems) def getitems(self): self.items.clear() self.items.addItems(self.list.currentText()) self.getSingleItem.clicked.connect(self.getitem) def getitem(self): self.item_id = self.items.currentText() print(self.item_id) app = QApplication(sys.argv) gui = UpdateItem() gui.show() app.exec_()
Apparently you are adding a new connection to getsingleitem.clicked
every time you run getitems
,
So the clicked
signal is connected to the same slot multiple times,
This results in the behavior you observed.
Mobile Line
self.getSingleItem.clicked.connect(self.getitem)
Going from getitems
to __init__
should solve this problem, I guess.
The above is the detailed content of PyQt4 button click register multiple times when clicked. For more information, please follow other related articles on the PHP Chinese website!