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

WBOY
發布: 2024-02-09 18:00:05
轉載
616 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!