具有 3 个不同选项的按钮
Button with 3 different options
有没有办法让一个按钮有两个选项?
我想要一个按钮,在选项一上,按钮会将其颜色更改为红色,在选项二上更改为黄色,在选项三上更改为绿色。再次按下(第 4 次)将从头开始(红色 --> 黄色 --> 绿色)。
到目前为止,我只能为按钮设置 2 个选项,但我创建的 destopapplication 的用户应该有 3 个选项。
到目前为止,这是我的代码(仅供参考:按钮在 QTableWidget 内,因此范围内的 x):
for x in range(cell_num):
self.button = QPushButton(' ',self)
self.button.setFlat(True)
self.table.setCellWidget(x, 5, self.button) # lights_button
self.button.clicked.connect(
lambda state, w=self.table.cellWidget(x, 5), r=x, c=5: self.button_pushed(w, r, c)
)
def button_pushed(self, w, r, c):
if w.text() != " ":
w.setIcon(QIcon("..\image\green_button.png"))
w.setText(" ")
elif w.text() == "Likes/UnLikes/Normal":
w.setIcon(QIcon("..\image\yellow_button.png"))
w.setText(" Likes/Normal")
else:
w.setIcon(QIcon("..\image\darkred_button.png"))
w.setText(" ")
你必须实现一个状态机,在这种情况下最好创建一个自定义按钮。
from itertools import cycle
from PyQt5 import QtCore, QtGui, QtWidgets
class Button(QtWidgets.QPushButton):
RedState, YellowState, GreenState = range(3)
def __init__(self, *args, **kwargs):
super(Button, self).__init__(*args, **kwargs)
self._current_state = Button.RedState
self._states = cycle([Button.RedState, Button.YellowState, Button.GreenState])
self.on_clicked()
self.clicked.connect(self.on_clicked)
def update_button(self):
if self._current_state == Button.RedState:
self.setIcon(QtGui.QIcon(r"..\image\green_button.png"))
self.setText(" ")
elif self._current_state == Button.YellowState:
self.setIcon(QtGui.QIcon(r"..\image\yellow_button.png"))
self.setText(" Likes/Normal")
elif self._current_state == Button.GreenState:
self.setIcon(QtGui.QIcon(r"..\image\darkred_button.png"))
self.setText(" ")
@QtCore.pyqtSlot()
def on_clicked(self):
self._current_state = next(self._states)
self.update_button()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.table = QtWidgets.QTableWidget(10, 10)
self.setCentralWidget(self.table)
for x in range(self.table.rowCount()):
button = Button(flat=True)
self.table.setCellWidget(x, 5, button)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
有没有办法让一个按钮有两个选项? 我想要一个按钮,在选项一上,按钮会将其颜色更改为红色,在选项二上更改为黄色,在选项三上更改为绿色。再次按下(第 4 次)将从头开始(红色 --> 黄色 --> 绿色)。
到目前为止,我只能为按钮设置 2 个选项,但我创建的 destopapplication 的用户应该有 3 个选项。 到目前为止,这是我的代码(仅供参考:按钮在 QTableWidget 内,因此范围内的 x):
for x in range(cell_num):
self.button = QPushButton(' ',self)
self.button.setFlat(True)
self.table.setCellWidget(x, 5, self.button) # lights_button
self.button.clicked.connect(
lambda state, w=self.table.cellWidget(x, 5), r=x, c=5: self.button_pushed(w, r, c)
)
def button_pushed(self, w, r, c):
if w.text() != " ":
w.setIcon(QIcon("..\image\green_button.png"))
w.setText(" ")
elif w.text() == "Likes/UnLikes/Normal":
w.setIcon(QIcon("..\image\yellow_button.png"))
w.setText(" Likes/Normal")
else:
w.setIcon(QIcon("..\image\darkred_button.png"))
w.setText(" ")
你必须实现一个状态机,在这种情况下最好创建一个自定义按钮。
from itertools import cycle
from PyQt5 import QtCore, QtGui, QtWidgets
class Button(QtWidgets.QPushButton):
RedState, YellowState, GreenState = range(3)
def __init__(self, *args, **kwargs):
super(Button, self).__init__(*args, **kwargs)
self._current_state = Button.RedState
self._states = cycle([Button.RedState, Button.YellowState, Button.GreenState])
self.on_clicked()
self.clicked.connect(self.on_clicked)
def update_button(self):
if self._current_state == Button.RedState:
self.setIcon(QtGui.QIcon(r"..\image\green_button.png"))
self.setText(" ")
elif self._current_state == Button.YellowState:
self.setIcon(QtGui.QIcon(r"..\image\yellow_button.png"))
self.setText(" Likes/Normal")
elif self._current_state == Button.GreenState:
self.setIcon(QtGui.QIcon(r"..\image\darkred_button.png"))
self.setText(" ")
@QtCore.pyqtSlot()
def on_clicked(self):
self._current_state = next(self._states)
self.update_button()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.table = QtWidgets.QTableWidget(10, 10)
self.setCentralWidget(self.table)
for x in range(self.table.rowCount()):
button = Button(flat=True)
self.table.setCellWidget(x, 5, button)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())