【发布时间】:2019-05-18 20:05:06
【问题描述】:
我想在单击工具栏中更改 QAction 的图标。
我在 C++ 中看到过同样的问题,但我很难理解其他语言。 (On Qt, how to change the icon of an action in the toolbar at runtime?)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.PausePlay = QAction(QIcon('Play.png'), 'Play')
self.PausePlay.setCheckable(True)
self.PausePlay.triggered[bool].connect(self.Playing)
self.toolbar = self.addToolBar('tb')
self.toolbar.addAction(self.PausePlay)
self.setGeometry(300, 300, 300, 300)
self.show()
def Playing(self, active):
if active:
# setting new icon
else:
# setting new icon
if __name__ == '__main__':
app = QApplication(sys.argv)
MWindow = MainWindow()
sys.exit(app.exec_())
【问题讨论】: