V
V
Vladislav Tikhonov2017-02-12 11:53:29
PyQt
Vladislav Tikhonov, 2017-02-12 11:53:29

PyQt4. How to change the value of a variable when selecting an action from QMenuBar?

Hello. Guys, tell me how to process the choice of the QMenuBar action.
I have a processing function, QMenuBar and action (Action) to it. I need a function to be called when the action is clicked.
Here's an example:

......
def example_func():
    print("example!")
example_menubar = QtGui.QMenuBar(self)
example_menu = example_menubar.addMenu('examplemenu')
example_action = example_menu.addAction('ExampleAction1')
example_action2 = example_menu.addAction('ExampleAction2')

Now let's say I need to call the example_func () function when selecting example_action2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-02-12
@wolterx

from PyQt4.QtGui import *

app = QApplication([])
win = QMainWindow()
mainMenu = win.menuBar()
mainMenu.setNativeMenuBar(False)
exampleMenu = mainMenu.addMenu('&Example')
item1 = QAction(QIcon(''), '&Item1', win)
item1.triggered.connect(lambda: print("example!"))
exampleMenu.addAction(item1)
win.show()
app.exec_()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question