user3891489
user3891489

Reputation: 61

PyQT - Function to change background image of widget just blanking background, not replacing it...?

I have this but it's not working correctly:

def changebackground(self):
 fname = QtGui.QFileDialog.getOpenFileName(self, 'Select background image', '/home/stu/Desktop/python')
 print fname
 self.results.setStyleSheet("background-image: url(fname); background-repeat: no-repeat; background-position: center;")

The correct fname is printed to console when the function is called via a button, but the background image of the results widget (a textedit box) just goes blank and loses the background image that I had started it with. It doesn't display the new chosen background.

Upvotes: 1

Views: 4719

Answers (1)

Emi
Emi

Reputation: 545

There is a problem with the stylesheet string! Since fname is a variable name and you are including it inside two " " You are writing "fname" directly to it, you can change it to:

self.results.setStyleSheet("background-image: url(" + fname + "); background-repeat: no-repeat; background-position: center;")

And it should work.

Upvotes: 1

Related Questions