PyQt4 button click register multiple times when clicked

WBOY
Release: 2024-02-09 18:00:05
forward
568 people have browsed it

PyQt4 button click register multiple times when clicked

Question content

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_()
Copy after login


Correct Answer


Apparently you are adding a new connection togetsingleitem.clickedevery time you rungetitems, So theclickedsignal is connected to the same slot multiple times, This results in the behavior you observed.

Mobile Line

self.getSingleItem.clicked.connect(self.getitem)
Copy after login

Going fromgetitemsto__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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!