Changing the Digit Color of QLCD Number

2020-07-09 09:16发布

I want to change the background and digit color of QLCDNumber in Qt Designer and I am going to use that design(GUI) on my Python program.

Some people said, that can get my adding style sheet in Qt-Designer.

QLCDNumber{color:rgb(85, 85, 255);background-color:rgb(0, 170, 255);}

It is successful for background color not for digit color.

How do I try for getting digit color

Thanks


I can't used the last two setColor(for light border and dark border). I was generating the python code(for GUI) from Qt4 Designer with pyuic4 tool. I added the codes into my python code file (ending with .py not .ui) as follows

self.palette = self.withpalette.palette()
self.palette.setColor(QtGui.Palette.WindowText,QtGui.QColor(85,85,255))
self.palette.setColor(QtGui.Palette.Background,QtGui.QColor(0,170,255))
self.palette.setColor(QtGui.Palette.Light,QtGui.QColor(255,0,0))
self.palette.setColor(QtGui.Palette.Dark,QtGui.QColor(0,255,0))
self.withpalette.setPalette(self.palette)

标签: python pyqt4
2条回答
Animai°情兽
2楼-- · 2020-07-09 09:52

Stylesheet for QtLCDNumber

QLCDNumber{
  background-color: rgb(0, 0, 0);
  border: 2px solid rgb(113, 113, 113);
  border-width: 2px;
  border-radius: 10px;
  color: rgb(255, 255, 255);
}
查看更多
女痞
3楼-- · 2020-07-09 10:09

Actually, it works. QLCDNumber, by default, paints digits in "raised" style. For small sizes, these borders that give the raised effect will mostly cover the digit and you won't see the normal color. If you make it larger, it will show:

QLCDNumber

If you don't want this "raised" effect, you can turn it off with setSegmentStyle:

lcd.setSegmentStyle(QtGui.QLCDNumber.Flat)

Flat QLCDNumber

On the other hand, if you want the "raised" effect but want to control it, you need to do it via QPalette. QPalette.Light and QPalette.Dark are the two colors that control those borders.

# get the palette
palette = lcd.palette()

# foreground color
palette.setColor(palette.WindowText, QtGui.QColor(85, 85, 255))
# background color
palette.setColor(palette.Background, QtGui.QColor(0, 170, 255))
# "light" border
palette.setColor(palette.Light, QtGui.QColor(255, 0, 0))
# "dark" border
palette.setColor(palette.Dark, QtGui.QColor(0, 255, 0))

# set the palette
lcd.setPalette(palette)

QLCDNumber custom QPalette

查看更多
登录 后发表回答