• Pyqt 获取动态生成的QLineEdit值


    动态生成控件参考上一篇: http://www.cnblogs.com/dcb3688/p/4588814.html

    那如何获取动态生成控件的值呢?

    比如,动态的生成10个输入框QLineEdit,输入值后,要获取每一个LineEdit的值。 在这里我们通过字典dict{} 来存储控件,循环dict 获取控件的text()

    #!/usr/bin/python  
    # -*-coding:utf-8-*-
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    # 主入口文件
    class MainWidget(QDialog):
        def __init__(self, parent=None):
            super(MainWidget, self).__init__(parent)
            self.setMinimumSize(100, 100)
            self.setWindowFlags(Qt.CustomizeWindowHint|Qt.WindowCloseButtonHint)
            self.setWindowOpacity(0.9)
    
            # 添加头部group
            self.headDict = {}
            self.headPostArrayKey = 0
            self.HeadGroupBox = QGroupBox(u'动态添加控件数据')
            self.HeadGroupBox.setMinimumHeight(100)  #高度最小值
            self.HeadGroupBox.scroll(100,2)
            self.HeadAddParam = QPushButton(u'+')
            self.headDict[str(self.headPostArrayKey)+'_key'] = QLineEdit()
            self.headDict[str(self.headPostArrayKey)+'_value'] = QLineEdit()
            self.HeadGroupBoxLayout = QGridLayout()
            self.HeadGroupBoxLayout.addWidget(self.HeadAddParam, 0, 0)
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Key:'), 1, 0)
            self.HeadGroupBoxLayout.addWidget(self.headDict[str(self.headPostArrayKey)+'_key'], 1, 1)
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Value:'), 1, 2)
            self.HeadGroupBoxLayout.addWidget(self.headDict[str(self.headPostArrayKey)+'_value'], 1, 3)
            self.HeadGroupBox.setLayout(self.HeadGroupBoxLayout)
            self.HeadAddParam.clicked.connect(self.addHeadParam)
    
    
            # 提交按钮
            self.btnPost = QPushButton(u'提交')
            self.postbtnLoayout = QHBoxLayout()
            self.postbtnLoayout.addStretch()
            self.postbtnLoayout.addWidget(self.btnPost)
            # Main布局
            main_layout = QVBoxLayout()
            main_layout.addWidget(self.HeadGroupBox)
            main_layout.addLayout(self.postbtnLoayout)  # addLayout 添加的是 Layout
            main_layout.setSpacing(0)
            self.setLayout(main_layout)
    
            self.connect(self.btnPost, SIGNAL('clicked()'), self.postData)
    
    
        def postData(self):
            self.headdictdata={}
            for k, v in self.headDict.items():
                temp=k.split('_')
                if temp[1]=='key':
                    if self.headdictdata.has_key(temp[0]):
                        self.headdictdata[temp[0]]['key'] =str(v.text())
                    else:
                        self.headdictdata[temp[0]] = {'key':str(v.text())}
    
                elif temp[1]=='value':
                    if self.headdictdata.has_key(temp[0]):
                        self.headdictdata[temp[0]]['value'] =str(v.text())
                    else:
                        self.headdictdata[temp[0]] = {'value':str(v.text())}
    
            print(self.headdictdata)
    
        # 添加头部Data
        def addHeadParam(self):
            sts=str(self.headPostArrayKey+1)
            self.headDict[sts+'_key'] = QLineEdit(sts+'name')
            self.headDict[sts+'_value'] = QLineEdit(sts+'chrome')
    
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Key'))
            self.HeadGroupBoxLayout.addWidget(self.headDict[sts+'_key'])
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Value'))
            self.HeadGroupBoxLayout.addWidget(self.headDict[sts+'_value'])
            self.headPostArrayKey+=1
    
    
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv) 
        main_widget = MainWidget()
        main_widget.show()
        sys.exit(app.exec_())

    效果:

    打印数据:

    {'1': {'value': '1chrome', 'key': '1name'}, '0': {'key': 'keyname', 'value': 'htmls'}, '3': {'value': '3chrome', 'key': '3name'}, '2': {'value': '2chrome', 'key': '2name'}}
  • 相关阅读:
    012_py之证书过期监测及域名使用的py列表的并集差集交集
    一些java的书籍
    java之内的工具分享,附带下载链接,方便以后自己寻找
    javascript的 == 与 === 的区别
    论httpclient上传带参数【commons-httpclient和apache httpclient区别】
    java操作小技巧,遇到过的会一直更新,方便查找
    常用jsp include用法,三种include的区别
    mybatis中的#和$的区别
    单例模式
    jQuery获取Select选择的Text和 Value(转)用时比较方便寻找
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4607945.html
Copyright © 2020-2023  润新知