如何在 PYQT initUI 函数中增加变量名? [英] How to increment the variable name in PYQT initUI function?

查看:111
本文介绍了如何在 PYQT initUI 函数中增加变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码如下:

class Example(QWidget):

    def __init__(self, fname):
        self.fname=fname
        super().__init__()
        self.initUI()


    def initUI(self):
    
        #self.cb1 = QCheckBox('all',self)

                
        self.cb2 = QCheckBox('a',self)
        self.cb3 = QCheckBox('b',self)
        self.cb4 = QCheckBox('c',self)
        #for x in range(1, 13): 
         #   locals()['self.cb{0}'.format(x)] = QCheckBox('a',self)
        
            
        bt = QPushButton('submit',self) 

        self.resize(300,200)
        self.setWindowTitle('sheets selction')

我试图通过输入循环量来增加 self.cb,所以我想像 self 一样增加循环中的 self.cb.cb1self.cb2self.cb3 ...,我尝试使用 locals(),它的工作原理是PYQT,但是当我尝试在 initUI 中使用它时,它不起作用,有人知道如何增加它吗?

I am trying to increment the self.cb by input amount of loop, so I want to increment the self.cb in the loop like self.cb1, self.cb2, self.cb3 ..., I tried to use locals(), it works out of the PYQT, but when I try to use it in the initUI, it doesnt work, does anyone know how to increment it?

推荐答案

我要分享我的想法.将复选框存储在列表中怎么样?甚至是字典:

I am gonna share my idea. What about storing the checkboxes in list? or even dictionary:

self.checkboxes_dictionary = {}
self.checkboxes_list = []
names = ["a", "b", "c", "d"]
for name in names:
    self.checkboxes_dictionary[name] = QCheckBox(name, parent=self)
    self.checkboxes_list.append(QCheckBox(name, parent=self))

这篇关于如何在 PYQT initUI 函数中增加变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆