Python 如何从 groupBox 更改子 QLabel 小部件的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32724169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:08:32  来源:igfitidea点击:

How to change font size of child QLabel widget from the groupBox

pythonpython-2.7pyqt4qlabel

提问by user2345

How can use different font & size for the child Widgets in the GroupBox and the tittle for the GroupBox in python

如何为 GroupBox 中的子 Widget 和 Python 中 GroupBox 的标题使用不同的字体和大小

def panel(self):
    groupBox = QtGui.QGroupBox("voltage Monitor")
    groupBox.setFont(QtGui.QFont('SansSerif', 13))       # the title size is good
    ..

    self.Voltage_Label = []
    ..

    vbox = QtGui.QGridLayout()
    self.Voltage_Label.append(QtGui.QLabel("voltage1 ")) # i need to have diff Font & size for these 
    self.Voltage_Label.append(QtGui.QLabel("voltage2 "))   
    self.Voltage_Label.append(QtGui.QLabel("voltage3 ")) 
    ..
    vbox.addWidget(self.Voltage_Label[i], i, 0)  
    ..
    groupBox.setLayout(vbox)
    return groupBox

I tired this

我累了

   self.Voltage_Label.setFont(QtGui.QFont('SansSerif', 10))

I get this error

我收到这个错误

    !! self.Voltage_Label.setFont(QtGui.QFont('SansSerif', 10))
AttributeError: 'list' object has no attribute 'setFont' !!

but for something like thistitle1 = QtGui.QLabel("Sample Title")as a child widget i can change it by

但是对于像这样title1 = QtGui.QLabel("Sample Title")的子小部件,我可以通过以下方式更改它

 title1.setFont(QtGui.QFont('SansSerif', 10))

采纳答案by user2345

While I was waiting for an answer I wanted to give it a try and found this method/solution for my question:

在等待答案时,我想尝试一下,并为我的问题找到了此方法/解决方案:

self.Voltage_Label = []

self.Voltage_Label.append(QtGui.QLabel("voltage1 ")) # i need to have diff Font & size for these 
self.Voltage_Label.append(QtGui.QLabel("voltage2 "))   
self.Voltage_Label.append(QtGui.QLabel("voltage3 "))
.
.

for i in xrange(5):
        newfont = QtGui.QFont("Times", 8, QtGui.QFont.Bold) 
        self.Voltage_Label[i].setFont(newfont)

回答by Nuno André

You were trying to invoke the method setFont()of an object of the class list(which hasn't this method), not of the QtGui.QLabelobject.

您试图调用setFont()类的对象的方法list(没有这个方法),而不是QtGui.QLabel对象的方法。

You can use a list comprehension for a better scalability and performance:

您可以使用列表理解来获得更好的可扩展性和性能:

voltages = ["voltage1 ", "voltage2 ", "voltage3 "]

# Populates the list with QLabel objects
self.Voltage_Label = [QtGui.QLabel(x) for x in voltages]

# Invokes setFont() for each object
[x.setFont(QtGui.QFont("Times", 8, QtGui.QFont.Bold)) for x in self.Voltage_Label]

If you need more voltage labels you only have to modify the list voltages.

如果您需要更多电压标签,您只需修改列表voltages

And then even:

然后甚至:

[vbox.addWidget(self.Voltage_Label[i], i, 0) for i in range(len(self.Voltage_Label))]