PDA

View Full Version : setStyleSheet doesnt work on raspberry pi



mrtyvz41
25th July 2019, 14:48
Hi to everyone,

i developing simple screen desing but i have a problem. I write on my desktop that base linux ubuntu 16.04 and pycharm editor, when i execute code everything awesome. But i try to run on raspberry pi same code, button.setStyleSheet("background-color: red") doesnt work. Moreover i dont get any eror,it comes screen but when i clicked to button, dont change button background color.

i want to change button background color when i clicked to button. This code running on my desktop.


def initUI(self):
self.btn.setToolTip('Buton1 islemleri') # Buton aciklamasini gosteriyor,uzerine gelindiginde.
self.btn.resize(120,70) # BOYUTLANDIRMA
self.btn.move(50, 50) # KONUMLANDIRMA
self.btn.clicked.connect(self.buton1_event) # button1 cikis olayi

def Ac(self,Cpin):
GPIO.output(Cpin,GPIO.HIGH)
def Kapat(self,Cpin):
GPIO.output(Cpin,GPIO.LOW)


# --------------Button1 Tiklama Olayi ------------------
def buton1_event(self):
if self.lcd1Bool == True:
self.lcd1.display(1)
self.Ac(kapaliCikis)
self.btn.setStyleSheet("background-color: yellow") # this line doesnt work !!!
self.lcd1Bool = False
else:
self.lcd1.display(0)
self.Kapat(kapaliCikis)
self.btn.setStyleSheet("background-color: #257c07") # this line doesnt work !!!
self.lcd1Bool = True

d_stranz
26th July 2019, 02:01
The ability to set background colors for buttons is OS dependent. If you are using Raspbian, you should do some searching to see if that OS allows such changes. I know that on Windows, it takes a lot of work to dynamically change background colors for buttons. You can find a lot of posts here concerning that.

mrtyvz41
26th July 2019, 08:01
Thank you for quickly reply, i will change rasp's operating system with ubuntu 18.04

anda_skoa
26th July 2019, 09:17
It is not so much operating system specific as it is style specific.

Make sure you run the same widget style on both platforms, e.g. Qt's default style "Fusion"

Cheers,
_

mrtyvz41
26th July 2019, 14:50
Hello again,

i install ubuntu-mate-18.04.2 on my raspberry but not change.

This is all of my code:



import sys
import RPi.GPIO as GPIO
import time
import subprocess
from PyQt5.QtWidgets import (QWidget, QToolTip,
QPushButton,QVBoxLayout, QApplication, QLCDNumber, QLabel)
from PyQt5.QtCore import *
from PyQt5.QtGui import *

GPIO.setmode(GPIO.BOARD)
#--------------Degiskenler---------------
#Girisler

#Cikislar
kapaliCikis= 16
yolcuCikis = 22
soforCikis = 24
geceLambaCikis = 26
aydinlatmaCikis = 32
hostesIkazCikis = 36


#-----------Tanimlamalar-----------------
#Giris
#GPIO.setup(fanButton,GPIO.IN)
#GPIO.setup(durdurButton,GPIO.IN)


#--------------Cikislar---------------------
GPIO.setup(kapaliCikis,GPIO.OUT)
GPIO.setup(yolcuCikis,GPIO.OUT)
GPIO.setup(soforCikis,GPIO.OUT)
GPIO.setup(geceLambaCikis,GPIO.OUT)
GPIO.setup(aydinlatmaCikis,GPIO.OUT)
GPIO.setup(hostesIkazCikis,GPIO.OUT)


class Example():



def __init__(self):
self.fanhiz = 10

self.dnm = QWidget()


#------------------BUTTONLAR--------------------------
self.btn = QPushButton('Kapali',self.dnm)
self.btn2 = QPushButton('Yolcu Alma',self.dnm)
self.btn3 = QPushButton('Sofor Kontrol',self.dnm)
self.btn4 = QPushButton('Gece Lamblari',self.dnm)
self.btn5 = QPushButton('Aydinlatma',self.dnm)
self.btn6 = QPushButton('Hostes ikaz',self.dnm)


#-------------- BUTTON 1 LCD GOSTERICISI---------------
self.lcd1 = QLCDNumber(self.dnm)
self.lcd1.resize(200,70)
self.lcd1.move(175,50)


#-------------BUTTON 2 LCD GOSTERICISI-----------------
self.lcd2 = QLCDNumber(self.dnm)
self.lcd2.resize(200,70)
self.lcd2.move(525,50)

#-------------BUTTON 3 LCD GOSTERICISI-----------------
self.lcd3 = QLCDNumber(self.dnm)
self.lcd3.resize(200,70)
self.lcd3.move(175,150)

#-------------BUTTON 4 LCD GOSTERICISI-----------------
self.lcd4 = QLCDNumber(self.dnm)
self.lcd4.resize(200,70)
self.lcd4.move(525,150)

#-------------BUTTON 5 LCD GOSTERICISI-----------------
self.lcd5 = QLCDNumber(self.dnm)
self.lcd5.resize(200,70)
self.lcd5.move(175,250)

#-------------BUTTON 6 LCD GOSTERICISI-----------------
self.lcd6 = QLCDNumber(self.dnm)
self.lcd6.resize(200,70)
self.lcd6.move(525,250)


#-------------LCD Dokunma islemi islemleri icin bool degiskenleri-----------------
self.lcd6Bool = True
self.lcd5Bool = True
self.lcd4Bool = True
self.lcd3Bool = True
self.lcd2Bool = True
self.lcd1Bool = True
self.btnState = False
self.initUI()




def initUI(self):

QToolTip.setFont(QFont('SansSerif', 10))
#--------------- Kapali -----------------------
self.btn.setToolTip('Buton1 islemleri') # Buton aciklamasini gosteriyor,uzerine gelindiginde.
self.btn.resize(120,70) # BOYUTLANDIRMA
self.btn.move(50, 50) # KONUMLANDIRMA
self.btn.clicked.connect(self.buton1_event) # button1 cikis olayi
self.btn.setAutoFillBackground(True)

#--------------- Yolcu Alma -----------------------
self.btn2.setToolTip('Buton2 islemleri')
self.btn2.setFont(QFont('Times',10))
self.btn2.resize(120,70)
self.btn2.move(400, 50)
self.btn2.clicked.connect(self.buton2_event) # button2 cikis olayi


#--------------- Sofor Kontrol -----------------------
self.btn3.setFont(QFont('Times',10))
self.btn3.resize(120,70)
self.btn3.move(50,150)
self.btn3.clicked.connect(self.buton3_event)


#--------------- Gece Lambalari -----------------------
self.btn4.setFont(QFont('Times',10))
self.btn4.resize(120,70)
self.btn4.move(400,150)
self.btn4.clicked.connect(self.buton4_event)


#--------------- Aydinlatma ---------------------------
self.btn5.setFont(QFont('Times',10))
self.btn5.resize(120,70)
self.btn5.move(50,250)
self.btn5.clicked.connect(self.buton5_event)

#--------------- Hostes Ikaz(imput) -----------------------
self.btn6.setFont(QFont('Times',10))
self.btn6.resize(120,70)
self.btn6.move(400,250)
self.btn6.clicked.connect(self.buton6_event)

#--------------- Cikis -----------------------
cikis = QPushButton('Cikis',self.dnm)
cikis.setToolTip('Uygulama Kapanir')
cikis.resize(120,70)
cikis.move(340,350)
cikis.clicked.connect(QApplication.instance().quit ) # cikis buttonu

self.dnm.setGeometry(300, 300, 800, 480)
#self.dnm.showFullScreen()
self.dnm.setWindowTitle('pi ekran')
#self.dnm.setWindowIcon(QIcon('/img/inovar.png')) # icon gostermiyor.!!
self.dnm.show()

def Ac(self,Cpin):
GPIO.output(Cpin,GPIO.HIGH)
def Kapat(self,Cpin):
GPIO.output(Cpin,GPIO.LOW)


# --------------Button1 Tiklama Olayi ------------------
def buton1_event(self):
if self.lcd1Bool == True:
self.lcd1.display(1)
self.Ac(kapaliCikis)
self.btn.setStyleSheet("background-color: yellow")
self.lcd1Bool = False
else:
self.lcd1.display(0)
self.Kapat(kapaliCikis)
self.btn.setStyleSheet("background-color: #257c07")
self.lcd1Bool = True

# --------------Button2 Tiklama Olayi ------------------
def buton2_event(self):
if self.lcd2Bool == True:
self.lcd2.display(1)
self.Ac(yolcuCikis)
self.lcd2Bool = False
else:
self.lcd2.display(0)
self.Kapat(yolcuCikis)
self.lcd2Bool = True


# --------------Button3 Tiklama Olayi ------------------
def buton3_event(self):
if self.lcd3Bool == True:
self.lcd3.display(1)
self.Ac(soforCikis)
self.lcd3Bool = False
else:
self.lcd3.display(0)
self.Kapat(soforCikis)
self.lcd3Bool = True


#--------------Button4 Tiklama Olayi ------------------
def buton4_event(self):
if self.lcd4Bool == True:
self.lcd4.display(1)
self.Ac(geceLambaCikis)
self.lcd4Bool = False
else:
self.lcd4.display(0)
self.Kapat(geceLambaCikis)
self.lcd4Bool = True


#--------------Button5 Tiklama Olayi ------------------
def buton5_event(self):
if self.lcd5Bool == True:
self.lcd5.display(1)
self.Ac(aydinlatmaCikis)
self.lcd5Bool = False
else:
self.lcd5.display(0)
self.Kapat(aydinlatmaCikis)
self.lcd5Bool = True

#--------------Button6 Tiklama Olayi ------------------
def buton6_event(self):
if self.lcd6Bool == True:
self.lcd6.display(1)
self.Ac(hostesIkazCikis)
self.lcd6Bool = False
else:
self.lcd6.display(0)
self.Kapat(hostesIkazCikis)
self.lcd6Bool = True

















if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
"""

dnm = QWidget()
QToolTip.setFont(QFont('SansSerif', 10))
dnm.setGeometry(300, 300, 800, 480)
dnm.setWindowTitle('pi ekran2')
dnm.show()
"""
sys.exit(app.exec_())





Video link:
https://www.youtube.com/watch?v=NncZHmw7d1E&feature=youtu.be

d_stranz
26th July 2019, 18:55
Instead of using a style sheet, can you try changing the color using the QPalette? In C++, this would be:



QPalette pal = button.palette();
pal.setColor( QPalette::Window, QColor( "yellow" ) );
button.setPalette( pal );


If this works, it might indicate that style sheets are not fully implemented on your RasPi's version of Qt. If it doesn't work, then it could be something to do with the default style as anda_skoa suggests.

mrtyvz41
30th July 2019, 09:16
Thank you for your reply

I dont solve this problem, i used text label instead of button. Thats enough for me..