使用循环设置多个 QLineEdit 的属性 [英] Set properties of multiple QLineEdit using a loop

查看:71
本文介绍了使用循环设置多个 QLineEdit 的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 for 循环设置多个 setFixedHeight() 属性:

I was wondering if it is possible to set multiple setFixedHeight() properties using a for loop:

for num in range(1, 6):
    self.LineEdit[num].setFixedHeight()

目前我有十二个 QLineEdit 框

currently I have twelve QLineEdit boxes

LineEdit1, LineEdit2, ... , LineEdit12 我希望用更少的代码来做到这一点.我尝试了上述方法,但它没有像我预期的那样遍历 LineEdit 框.self.LineEdit[num] 只能用于列表吗?

LineEdit1, LineEdit2, ... , LineEdit12 and I'm hoping to do this with less code. I tried the above method, and it did not iterate through the LineEdit boxes as I had expected. Would self.LineEdit[num] only work for a list?

推荐答案

对于此任务,您可以使用 getattr():

for this task you can use getattr():

for i in range(1,13):
    getattr(self, "LineEdit{}".format(i)).setFixedHeight(10)

这篇关于使用循环设置多个 QLineEdit 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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