2014-05-18 97 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)')