我创建了一个窗口并为其添加了背景图像。我还在窗口中央添加了一个标签,我的问题是标签周围有一个奇怪的框,好像它有一种奇怪的背景颜色。我尝试使用这行代码修复它:
headline.setStyleSheet("background-color: transparent;")
但它什么也没做
这是我的代码:
import sys
from PyQt5.QtWidgets import QApplication, QDesktopWidget, QWidget, QLabel
from PyQt5.QtGui import QFont
BACKGROUND_IMAGE_PATH = "./documents/background_image.jpg"
def setWidgets(window: QWidget, window_geo: list):
# Create the headline text
headline = QLabel(window)
headline.setText("A big lable here")
font = QFont("Times New Roman")
# Set font size to 60% of the window height
font.setPointSize(int(window_geo[3] * 0.06))
# Set the background color of the label to transparent
headline.setStyleSheet("QLabel {\n"
" background-color: transparent;\n"
"}")
headline.setFont(font)
headline.adjustSize()
headline.move((window_geo[2] - headline.width()) // 2,
window_geo[1] + window_geo[3] // 5 - headline.height() // 2)
def getWindowValues():
"""
getWindowValues getting the screen (width and height) and reduce it by 25 present to set the size of the app window
"""
desktop = QDesktopWidget()
screen_width = desktop.screenGeometry().width()
screen_height = desktop.screenGeometry().height()
window_width = int(screen_width * 0.75)
window_height = int(screen_height * 0.80)
# setting the x and y to be in the middle of the screen
x = int(((screen_width - window_width) / 2) * 0.1)
y = int((screen_height - window_height) / 2)
return x, y, window_width, window_height
def main():
stylesheet = f"QWidget {{background-image: url('{BACKGROUND_IMAGE_PATH}'); background-repeat: no-repeat; background-position: center;}}"
app = QApplication(sys.argv)
app.setStyleSheet(stylesheet)
# getting the geometry of the window
x, y, window_width, window_height = getWindowValues()
window = QWidget()
window.setWindowTitle("App name")
setWidgets(window, [x, y, window_width, window_height])
window.setGeometry(x, y, window_width, window_height)
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
headline.setStyleSheet("background-color: rgba(0,0,0,0);")
headline.setAutoFillBackground(False)