python - 选择按钮时如何更改按钮颜色以及我只想使用pyqt4选择一个按钮的每个布局?

标签 python pyside

这是我的示例代码。我有一个水平和垂直的行,我将在水平和垂直方向上使用我的文本。当我选择布局中的任何按钮时,当我选择另一个按钮时,我想将那个关键按钮更改为蓝色,该按钮会自动更新蓝色,而前一个按钮将变为原始颜色 下面是我的代码:

import sys
from PySide import QtGui,QtCore
from functools import partial
class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        self.hbox = QtGui.QHBoxLayout()
        self.v1box = QtGui.QVBoxLayout()
        self.v2box = QtGui.QVBoxLayout()
        self.labl = QtGui.QLabel("horizontal")
        self.v1box.addWidget(self.labl)
        for key in ['1', '2', '3', '4', '5', '6']:
            self.btns1 = QtGui.QPushButton(str(key))
            self.btns1.clicked.connect(partial(self.text1, key, "horizontal"))
            self.v1box.addWidget(self.btns1)
        self.lab2 = QtGui.QLabel("vertical")
        self.v2box.addWidget(self.lab2)
        for key in ['1', '2', '3', '4', '5', '6']:
            self.btns2 = QtGui.QPushButton(str(key))
            self.btns2.clicked.connect(partial(self.text2, key,"Vertical"))
            self.v2box.addWidget(self.btns2)
        self.hbox.addLayout(self.v1box)
        self.hbox.addLayout(self.v2box)
        self.setLayout(self.hbox)
    def text1(self,key,type):
        if type == "horizontal":
            X = int(str(key))
            print X, "xxx"
    def text2(self,key,type):
        if type == "vertical":
            Y = int(str(key))

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

最佳答案

您可以启用 QPushButton 的 checkeable 属性并使用 QButtonGroup 以便仅选中其中一个。然后使用已检查的伪状态设置所需的样式。

import sys
from PySide import QtCore, QtGui 
from functools import partial

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setStyleSheet("QPushButton::checked{ background-color:blue; border: none; }")
        v1box = QtGui.QVBoxLayout()
        v1box.addWidget(QtGui.QLabel("horizontal"))
        group1 = QtGui.QButtonGroup(self)
        for key in ['1', '2', '3', '4', '5', '6']:
            btn = QtGui.QPushButton(str(key), checkable=True)
            btn.clicked.connect(partial(self.text1, key, "horizontal"))
            v1box.addWidget(btn)
            group1.addButton(btn)

        v2box = QtGui.QVBoxLayout()
        v2box.addWidget(QtGui.QLabel("vertical"))
        group2 = QtGui.QButtonGroup(self)
        for key in ['1', '2', '3', '4', '5', '6']:
            btn = QtGui.QPushButton(str(key), checkable=True)
            btn.clicked.connect(partial(self.text2, key,"vertical"))
            v2box.addWidget(btn)
            group2.addButton(btn)

        hbox = QtGui.QHBoxLayout(self)
        hbox.addLayout(v1box)
        hbox.addLayout(v2box)

    def text1(self, key, direction):
        if direction == "horizontal":
            X = int(str(key))
            print(X, "xxx")

    def text2(self, key, direction):
        if direction == "vertical":
            Y = int(str(key))
            print(Y, "yyy")


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

关于python - 选择按钮时如何更改按钮颜色以及我只想使用pyqt4选择一个按钮的每个布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678596/

相关文章:

python - requirements.txt 与 setup.py

python - 使用 base64 库中的 b32decode 进行 base32 解码时忽略填充异常

python - 如何从类变量(从类外部)获取类实例?

python - 如何使用 QProcess 循环的输出更新 UI,而不卡住 UI?

python - QGraphicsScene 选中时改变对象

python - 为什么 PyQt 类的反弹方法会引发 TypeError

python - 带有列表字段的 Flask-Restful-Swagger 模型类

python - Flask-sqlalchemy : sqlalchemy. exc.InvalidRequestError

python - 如何将 Vector 拆分为列 - 使用 PySpark

python - Sqlite3 或 QtSql