简体   繁体   English

PyQt4如何获取复选框的“文本”

[英]PyQt4 How to get the “text” of a checkbox

So I'm trying to add the "text" associated with a checked checkbox to a list as soon as they're checked, and I'm doing this: 因此,我尝试将与选中的复选框相关联的“文本”添加到列表中,然后立即将它们添加到列表中,我正在这样做:

class Interface(QtGui.QMainWindow):

    def __init__(self):
        super(Interface, self).__init__()
        self.initUI()
        self.shops=[]

    def initUI(self):
        widthPx = 500
        heightPx = 500

        self.setGeometry(100,100,widthPx,heightPx)

        #menus
        fileMenu = menuBar.addMenu("&File")
        helpMenu = menuBar.addMenu("&Help")

        #labels
        shopList = _getShops()
        for i, shop in enumerate(shopList):
            cb = QtGui.QCheckBox(shop, self)
            cb.move(20, 15*(i)+50)
            cb.toggle()
            cb.stateChanged.connect(self.addShop)



        self.setWindowTitle("Absolute")
        self.show()

    def addShop(self, state):

        if state == QtCore.Qt.Checked:
            #I want to add the checkbox's text
            self.shops.append('IT WORKS')
        else:
            self.shops.remove('IT WORKS')

But instead of adding "IT WORKS" I want to add the text associated with the checkbox that was just selected. 但是,我不想添加“ IT WORKS”,而是要添加与刚选中的复选框关联的文本。

I usually pass additionnal parameters in my signals/slots using partial Functools doc You can use it to pass your checkbox text. 我通常使用partial Functools文档在信号/插槽中传递附加参数。您可以使用它传递复选框文本。

First, import partial: 首先,导入部分:

from functools import partial

Then, change your connect() method and pass your checkbox text: 然后,更改您的connect()方法并传递复选框文本:

cb.stateChanged.connect( partial( self.addShop, shop) )

To finish, update your addShop() method: 最后,更新您的addShop()方法:

def addShop(self, shop, state):
    if state == Qt.Checked:
        self.shops.append(shop)
    else:
        try:
            self.shops.remove(shop)
        except:
            print ""

Notes: 笔记:

  • I've added a try/except at the end because your checkboxes are checked by default. 我在末尾添加了try / except,因为默认情况下会选中您的复选框。 When you uncheck them, it tries to remove an unknow item from your self.shops list. 当您取消选中它们时,它将尝试从self.shops列表中删除一个未知物品。

  • With this method, this is not the current checkbox text which is send to your method. 使用此方法,这不是发送到您的方法的当前复选框文本。 It it the first text that was used to initialize your checkboxes. 它是用于初始化您的复选框的第一个文本。 If, during the execution of your script, you modify the checkbox text, it will not be updated in your addShop method. 如果在脚本执行期间修改了复选框文本,则不会在addShop方法中对其进行更新。

Update: 更新:

In fact, you can pass your checkbox in the partial: 实际上,您可以在部分框中传递您的复选框:

cb.stateChanged.connect( partial( self.addShop, cb) )

and retrieve it this way: 并以这种方式检索:

def addShop(self, shop, state):
    if state == Qt.Checked:
        self.shops.append(shop.text())
    else:
        try:
            self.shops.remove(shop.text())
        except:
            print ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM