简体   繁体   English

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

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

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 this title1 = QtGui.QLabel("Sample Title") as a child widget i can change it by但是对于像这样的东西title1 = QtGui.QLabel("Sample Title")作为一个子小部件我可以改变它

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

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)

You were trying to invoke the method setFont() of an object of the class list (which hasn't this method), not of the QtGui.QLabel object.您试图调用类list (没有此方法)的对象的setFont()方法,而不是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))]

also, you can try另外,你可以试试

font = QtGui.QFont("Times", 8, QtGui.QFont.Bold)
[label.setFont(font) for label in self.Voltage_Label]

if you are creating a font object every time you iterate over an item of self.Voltage_Label it will cost you some memory.如果您每次迭代self.Voltage_Label的项目时都创建字体对象,它将花费您一些内存。 so, therefore, you can share the same one with all the labels.因此,您可以与所有标签共享同一个标签。 whenever memory matters you can use this technique.每当内存很重要时,您都可以使用此技术。 but if you want to change the font on all label objects it won't change the font in other QLabel objects.但是如果你想改变所有label对象的字体,它不会改变其他QLabel对象的字体。

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

相关问题 如何在.kv中更改ListView小部件的字体大小 - How to change the font size of a ListView widget in .kv 如何在切换按钮小部件中增加字体大小或更改描述的字体样式 - How to increase font size or change font style of description in togglebutton widget 如何在 PyQt5 QLabel(固定大小)中增加/减小字体大小? - How to Increase /decrease the font size In PyQt5 QLabel (Fixed Size)? 如何在不知道小部件的字体系列/大小的情况下更改小部件的字体样式? - How to change a widget's font style without knowing the widget's font family/size? 如何仅更改tkinter中的文本小部件的字体大小(而不是家庭) - How to only change font size of text widget in tkinter(not family) 如何“获取” QLabel小部件的属性 - How to “get” an attribute of a QLabel widget tkinter,如何防止Text widget在更改字体和设置widget大小时调整大小? - In tkinter, how to prevent Text widget from resizing when font is changed and set size for widget? 如何增加Text小部件的字体大小? - How to increase the font size of a Text widget? 如何增加文本小部件的字体大小? - how increase font size in text widget? Qt 布局不反映 groupbox 大小的变化 - Qt layout does not reflect groupbox size change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM