PyQt:如何給介面自定義背景?
阿新 • • 發佈:2018-12-27
原本的效果是這樣的:
我想變成這樣:
怎麼做呢?
有很多方法,這裡給出一種:
from PyQt5.QtGui import QPalette, QBrush, QPixmap from dx import Ui_Form class MyWindow(QtWidgets.QWidget,Ui_Form): def __init__(self): super(MyWindow,self).__init__() #註釋掉這一句,寫到InitUi方法裡面執行 #self.setupUi(self) #介面繪製交給InitUi方法 self.initUI() def add(self): self.helloworld.addItem(self.lineEdit.displayText()) def show_current(self): self.textBrowser.append(self.helloworld.currentText()) def initUI(self): #設定背景圖片 palette1 = QPalette() #填寫圖片的絕對路徑 palette1.setBrush(self.backgroundRole(), QBrush(QPixmap('G:\\image\\1.jpg'))) self.setPalette(palette1) #顯示視窗 --- 註釋 #self.show() self.setupUi(self) if __name__=="__main__": import sys app=QtWidgets.QApplication(sys.argv) myshow=MyWindow() myshow.show() sys.exit(app.exec_())