【问题标题】:QFrame Background Color overlapped with other Widgets like QLineEdit, QListBoxWidget, etc. How to avoid it?QFrame 背景颜色与 QLineEdit、QListBoxWidget 等其他 Widget 重叠。如何避免?
【发布时间】:2020-05-27 15:20:01
【问题描述】:

在我的程序中,

QLineEdit 和 QListWidget 放置在 QVBoxLayout 中。然后 QVBoxLayout 放入一个带有背景颜色样式表的 QFrame:橙色。

QLineEdit 和 QListWidget 也获得与 QFrame 相同的背景颜色。如何避免背景颜色重叠? .

假设,如果我们通过样式表改变QListwidget的背景颜色,那么滚动条颜色也改变为QListWidget颜色。

如何避免?,我需要原生样式布局?

import sys
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *

item = ["Python", "Python 2.7", "Python 2.9", "Python 3.5", "Python 3.7", "National", "Zebra",
                "Apple", "X Ray","Boat", "Tiger", "Item001", "Item002", "Item003", "Item004", "Item005",
                "001Item", "002Item", "003Item","004Item", "005Item", "Ball", "Cat", "Dog", "Fish",
                "Gold Fish", "Star Fish"]


class myList(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Frame Example")
        self.myui()

    def myui(self):

        self.textbox = QLineEdit(self)
        self.listbox = QListWidget(self)
        self.listbox.addItems(item)

        vbox = QVBoxLayout()
        vbox.addWidget(self.textbox)
        vbox.addWidget(self.listbox)

        frame = QFrame()
        frame.setLayout(vbox)
        frame.setStyleSheet("background-color:orange")

        main_layout =QHBoxLayout()
        main_layout.addWidget(frame)
        self.setLayout(main_layout)

def main():
    myapp = QApplication(sys.argv)
    mywin = myList()
    mywin.show()
    sys.exit(myapp.exec_())

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python pyqt pyqt5 qframe


    【解决方案1】:

    除了指明它将影响的类之外,您还必须设置一个选择器(例如 objectName):

    frame = QFrame()
    frame.setObjectName("frame")
    frame.setLayout(vbox)
    frame.setStyleSheet("QFrame#frame{background-color:orange}")

    有关更多详细信息,我建议阅读 Qt 文档:

    【讨论】:

    • hai @eyllanesc,假设如果我们从一个函数中调用一个样式表,那么我们如何制作它?例如 def func_Qframe_stylesheet_001(self): return """ QFrame{background-color: 'orange';} """
    • @Kumar 我不明白你
    • @eyllanesc,我将样式表设为函数/方法,然后调用它。现在,我该如何解决这个问题?
    • @Kumar 看来你对python不太了解(我建议检查你的笔记),因为你问的很琐碎:更改为return "QFrame#frame{background-color:orange}"并添加your_framesetObjectName("frame")
    • 是的,@eyllanesc 先生,您绝对正确。我是 Python 新手,不仅是 Python,我也是 Programme 新手,在像你这样知识渊博的人的帮助下,我会一步一步地移动。非常感谢。
    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 2011-04-29
    相关资源
    最近更新 更多