2014-05-18 94 views
1

如何在python中将背景图片设置为QTextEdit窗口?将背景图片设置为QTextEdit

到目前为止,我的课是这样的:

class MoveText(QtGui.QTextEdit): 

    def __init__(self, example_text): 
     super(MoveText, self).__init__()  
     self.setText(example_text) 
     self.initUI() 



    def initUI(self):  
     self.setGeometry(400, 300, 400, 400) 
     self.setWindowTitle('MoveText') 
     self.setFocusPolicy(QtCore.Qt.StrongFocus) 
     self.show() 
    ... 

我可以设置通过(WWW)网址的图片?

回答

1

使用setStyleSheet

def initUI(self): 
    ... 
    self.setStyleSheet("background-image: url(Qt-logo.png)") 
    self.show() 

根据简单的实验,好像setStyleSheet与外部URL不起作用。

您可能需要自行下载映像文件。例如:

import urllib 

... 

url = 'http://qt-project.org/images/qt13a/Qt-logo.png' 
with open('background.png', 'wb') as f: 
    f.write(urllib.urlopen(url).read()) 
self.setStyleSheet('background-image: url(background.png)')