Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get right click context menu in Windows application using Python?

How to get the right click context menu in a Windows application using Python, do not set the position of the cursor and when not focus that Windows application.

Context menu is not the Explorer context menu, it is the Windows application right click context menu.

enter image description here

like image 882
최경우 Avatar asked Sep 20 '26 20:09

최경우


1 Answers

Here is an example for Notepad:

app.UntitledNotepad.Edit.Click(button='right') # works
app.PopupMenu.MenuSelect('Paste') # seems not working when Notepad is not in focus
# though it works when app.UntitledNotepad.SetFocus() is called before

app.PopupMenu.MenuSelect('Paste') may not work in such a case because probably WM_COMMAND can be sent to a focused window only. To get it to work use app.PopupMenu.MenuItem('Paste').ClickInput() though your app window will get to focus any way.


So finally there are 2 working examples. The first is:

app.UntitledNotepad.SetFocus()
app.UntitledNotepad.Edit.Click(button='right')
app.PopupMenu.MenuSelect('Paste')

The second is:

app.UntitledNotepad.Edit.Click(button='right')
app.PopupMenu.MenuItem('Paste').ClickInput()
like image 127
Vasily Ryabov Avatar answered Sep 22 '26 10:09

Vasily Ryabov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!