简体   繁体   English

PyQt4-工具栏中重复了Python图像

[英]PyQt4 - Python Image is repeated in toolbar

I am currently working on a small application to do with arranging music lessons and am trying to add a background image. 我目前正在开发一个小型应用程序,用于安排音乐课程,并且正在尝试添加背景图片。 When I add an image it also repeats the image in the menu bar and toolbar. 当我添加图像时,它还会在菜单栏和工具栏中重复该图像。 How can I stop this from happening? 我该如何阻止这种情况的发生?

Thanks in advance! 提前致谢!

    import sys
from PyQt4 import QtGui

class windowMain(QtGui.QMainWindow):

    def __init__(self):
        super(windowMain, self).__init__()

        self.initUI()

    def initUI(self):

#Layout of window
        self.resize(700, 500) #Maximizing main window
        self.center()
        self.setWindowTitle('Lesson Planner') #Setting window title
        self.setStyleSheet("border-image: url(p.jpg);")       
        self.show() #Showing the window

If you don't specify which widget(s) the stylesheet applies to, it will cascade to all child widgets (ie just like CSS). 如果您未指定样式表适用于哪个窗口小部件,它将级联到所有子窗口小部件(即,就像CSS一样)。 So, as with CSS, you need to use the right selector syntax to apply the background image to the appropriate widget. 因此,与CSS一样,您需要使用正确的选择器语法将背景图像应用于适当的小部件。

For you example, one way to do that would be to set the objectName for the central widget of the main window, and then use that name in the selector: 例如,一种方法是为主窗口的中央窗口小部件设置objectName ,然后在选择器中使用该名称:

    def initUI(self):
        self.centralWidget().setObjectName('CentralWidget')
        self.setStyleSheet("""
            #CentralWidget { background-image: url(./image.png) }
            """)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM