当我 select 它和我想 select 一个按钮的每个布局仅使用 pyqt4 时,如何更改按钮颜色?
How can I change the push button colour when I select it and each layout i want to select one button only using pyqt4?
这是我的示例代码。我有一个水平和垂直的行,我将在水平和垂直方向上使用我的文本。当我 select 布局中的任何按钮时,当我选择另一个按钮时,我想将那个关键按钮更改为蓝色,该按钮会自动更新蓝色,而前一个按钮将变为原始颜色
以下是我的代码:
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 的可检查性 属性 并使用 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()
这是我的示例代码。我有一个水平和垂直的行,我将在水平和垂直方向上使用我的文本。当我 select 布局中的任何按钮时,当我选择另一个按钮时,我想将那个关键按钮更改为蓝色,该按钮会自动更新蓝色,而前一个按钮将变为原始颜色 以下是我的代码:
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 的可检查性 属性 并使用 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()