如何在不影响Pyqt5中的小部件的情况下将背景图像添加到主窗口

时间:2019-02-06 16:30:46

标签: python pyqt5

我想在主窗口中添加背景图像而不更改按钮的背景图像,并且还需要保持宽高比

我尝试过

self.centralWidget.setStyleSheet("background-image: url(The_Project_logo.png); background-repeat: no-repeat; background-position: center")

但这会更改所有小部件的背景图像

Actual output

1 个答案:

答案 0 :(得分:1)

尝试一下:

$UniqueNames = array();    

for($i=0;$i<count($file_array);$i++)
{
    if($file_array[$i]['error']){
        echo $phpFileUploadErrors[$file_array[$i]['error']];
    } else {
        $extensions = array('jpg','png','gif','jpeg');
        $file_ext = explode('.',$file_array[$i]['name']);
        $file_ext = end($file_ext);

        if (!in_array($file_ext, $extensions)){
            echo "Invalid file extension!";
        } else {
            $fileNameNew = uniqid('', true).".".$file_ext;
            $UniqueNames[] = $fileNameNew;

            move_uploaded_file($file_array[$i]['tmp_name'], 'Bilder/'.$fileNameNew);
            echo $phpFileUploadErrors[$file_array[$i]['error']];
        }
    }
}

enter image description here


from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)

        self.pushButton1 = QPushButton("Button 1", self.centralwidget)
        self.pushButton2 = QPushButton("Button 2", self.centralwidget)

        lay = QHBoxLayout(self.centralwidget)
        lay.addWidget(self.pushButton1)
        lay.addWidget(self.pushButton2)


stylesheet = """
    MainWindow {
        background-image: url("D:/_Qt/img/cat.jpg"); 
        background-repeat: no-repeat; 
        background-position: center;
    }
"""

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    app.setStyleSheet(stylesheet)     # <---
    window = MainWindow()
    window.resize(640, 640)
    window.show()
    sys.exit(app.exec_())