Home > Backend Development > Python Tutorial > PyQt4 button click register multiple times when clicked

PyQt4 button click register multiple times when clicked

WBOY
Release: 2024-02-09 18:00:05
forward
666 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 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)
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template