gpt4 book ai didi

python - 如何在运行时更改工具栏中 Action 的图标?

转载 作者:行者123 更新时间:2023-11-28 17:59:51 25 4
gpt4 key购买 nike

我想在点击工具栏时更改 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_())

最佳答案

如其他答案所示,没有必要使用 QToolButton。在这种情况下,您应该只获取 QAction 并设置一个新图标

def Playing(self, active):
self.PausePlay.setIcon(QIcon("icon1.png") if active else QIcon("icon2.png"))

关于python - 如何在运行时更改工具栏中 Action 的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com