首頁  >  文章  >  後端開發  >  PyQt4按鈕點擊時點擊註冊多次

PyQt4按鈕點擊時點擊註冊多次

WBOY
WBOY轉載
2024-02-09 18:00:05617瀏覽

PyQt4按鈕點擊時點擊註冊多次

問題內容

我是 pyqt4 的新手,經過多次搜尋後,我沒有找到有關我在 gui 中看到的問題的資訊。

問題是,當使用者點擊 getsingleitems 按鈕時,函數運行的次數與使用者點擊 getallitems 的次數相同。一個範例是,如果使用者點擊 getallitems 來填充 items 字段,然後點擊 getsingleitem,則 getitems 運行一次,並且根據期望列印一次結果。但是,如果使用者從清單中選擇另一個項目並再次按一下 getallitems,然後按一下 getsingleitem,結果是 getitem 運行 2x,因此列印 2x。每次運行都會增加,因此即使不更改選擇,單擊 getallitems 4x,然後單擊 getitem 將通過單擊 getsingleitem 來運行 4x。刷新它的唯一方法是關閉 gui 並重新打開。如有任何幫助,我們將不勝感激。

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_()

正確答案


顯然,您正在每次運行getitems 時添加到getsingleitem.clicked 的新連接, 因此 clicked 訊號多次連接到同一個插槽, 這會導致您觀察到的行為。

行動線路

self.getSingleItem.clicked.connect(self.getitem)

getitems__init__ 應該可以解決這個問題,我猜。

以上是PyQt4按鈕點擊時點擊註冊多次的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除