[英]Adding a picture to background of pyqt5 interface
如何使我放在文本編輯器旁邊的圖片出現在文本編輯器的背景中並填充窗口? 目前我只能並排顯示文本編輯器和圖像。
import sys
from PyQt5.QtWidgets import
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtPrintSupport import *
class pencere(QWidget):
def __init__(self): #yapici fonksiyon
app = QApplication(sys.argv)
super().__init__()
self.setGeometry(100,50,1080,1080)
self.setWindowTitle("M content re-writer")
self.texteditor()
self.image()
self.show()
app.exec_()
def texteditor(self):
self.editor=QTextEdit(self)
self.editor.resize(500,500)
self.editor.move(5,40)
button=QPushButton("re-write",self)
button.setStyleSheet("QPushButton" "{" "background-color : lightblue;" "}" "QPushButton::pressed" "{""background-color : red;" "}")
button.move(5,10)
button.clicked.connect(self.function)
def function(self):
text=self.editor.toPlainText() #editor'de yazan yaziyi al
path, _ = QFileDialog.getSaveFileName(self, "Save file", "", "Text documents (*.txt);All files (*.*)")
if text=="":
print("none")
else:
with open(path, 'w') as murti:
murti.write(text)
def image(self):
self.image=QLabel(self)
self.image.setPixmap(QPixmap("yaratici.jpeg"))
self.image.resize(900,500)
self.image.move(505,40)
pencere=pencere()
嘗試一下:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtPrintSupport import *
class Pencere(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100,50,1080,1080)
self.setWindowTitle("M content re-writer")
self.widget = QWidget(self)
self.widget.setObjectName("widget")
self.texteditor()
vbox2 = QVBoxLayout(self.widget)
vbox2.addWidget(self.button, alignment=Qt.AlignLeft)
vbox2.addWidget(self.editor, alignment=Qt.AlignLeft | Qt.AlignTop)
vbox = QVBoxLayout(self)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.addWidget(self.widget)
def texteditor(self):
self.editor = QTextEdit()
self.editor.resize(500, 200)
self.editor.move(5,40)
self.button = QPushButton("re-write")
self.button.clicked.connect(self.function)
def function(self):
text = self.editor.toPlainText() # editor'de yazan yaziyi al
# path, _ = QFileDialog.getSaveFileName(self, "Save file", "", "Text documents (*.txt);All files (*.*)")
if not text: # == "":
print("none")
return
# else:
path, _ = QFileDialog.getSaveFileName(
self,
"Save file",
"",
"Text documents (*.txt);All files (*.*)")
if path:
with open(path, 'w') as murti:
murti.write(text)
qss = """
#widget {
border-image: url(image.jpg) 0 0 0 0 stretch stretch;
}
QPushButton {background-color : lightblue;}
QPushButton:hover:pressed {background-color: red;}
QPushButton:hover {background-color: #0ff;}
QTextEdit {
background-image: url("rio.jpg");
min-width: 400px;
min-height: 400px;
border: 2px solid green;
color:red;
font-size:24px;
}
"""
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(qss)
demo = Pencere()
demo.show()
sys.exit(app.exec_())
使用 QStyleSheet 設置背景圖像。
self.editor.setStyleSheet('QTextEdit{background-image:url("yaratici.jpeg");}')
您可能必須提供圖像文件的絕對路徑。
也許您可以嘗試:
stylesheet = """
QWidget {
background-image: url("your path:/"yaratici.jpeg");
background-repeat: no-repeat;
background-position: center;
}
"""
並在適當的地方
self.setStyleSheet(stylesheet)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.