如何使放置在文本编辑器旁边的图片出现在文本编辑器的背景中并填充窗口?目前,我只能同时显示文本编辑器和图像。
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()
答案 0 :(得分:0)
使用QStyleSheet设置背景图像。
self.editor.setStyleSheet('QTextEdit{background-image:url("yaratici.jpeg");}')
您可能必须提供图像文件的绝对路径。
答案 1 :(得分:0)
也许您可以尝试:
stylesheet = """
QWidget {
background-image: url("your path:/"yaratici.jpeg");
background-repeat: no-repeat;
background-position: center;
}
"""
并在适当的地方放置
self.setStyleSheet(stylesheet)
答案 2 :(得分:0)
尝试一下:
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_())