I've made a Form
with an Image
using the DelphiFMX GUI Library for Python, but what I want now is a right-click context menu on the image. When I right-click on the image, then it should bring up a context popup menu as you see here in VSCode when I right-click:
I have the following code that makes my Form
and Image
:
import os
from delphifmx import *
class frmMain(Form):
def __init__(self, owner):
self.Caption = 'My Form with Image and Context Menu'
self.Width = 1000
self.Height = 1000
self.imgDirt = Image(self)
self.imgDirt.Parent = self
self.imgDirt.Align = "Client"
self.imgDirt.Margins.Top = 40
self.imgDirt.Margins.Left = 40
self.imgDirt.Margins.Right = 40
self.imgDirt.Margins.Bottom = 40
path = os.path.dirname(os.path.abspath(__file__))
self.imgDirt.Bitmap.LoadFromFile(path + '\dirt.png')
def main():
Application.Initialize()
Application.Title = "My Application"
Application.MainForm = frmMain(Application)
Application.MainForm.Show()
Application.Run()
Application.MainForm.Destroy()
main()
I tried doing things like this, but it doesn't work (NameError: name 'ContextMenu' is not defined
):
self.cm = ContextMenu(self)
self.cm.Items.Add("Item 1")
self.cm.Items.Add("Item 2")
self.cm.Items.Add("Item 3")
Same for:
self.cm = PopUpMenu(self)
How do I do this in FMX for Python? Simple Right-click Context Menu PopUp on the Image
I found my own problem. It is a PopupMenu
and not a PopUpMenu
. So the wording is case-sensitive.
To create a PopupMenu (Context Menu), here's the code:
# Step 1: Create a PopupMenu
self.cm = PopupMenu(self)
self.cm.Parent = self
# Step 2: Create MenuItems
self.MenuItem1 = MenuItem(self)
self.MenuItem1.Parent = self.cm
self.MenuItem1.Text = "Item 1"
self.MenuItem2 = MenuItem(self)
self.MenuItem2.Parent = self.cm
self.MenuItem2.Text = "Item 2"
self.MenuItem3 = MenuItem(self)
self.MenuItem3.Parent = self.cm
self.MenuItem3.Text = "Item 3"
# Step 3: Assign PopupMenu to Image
self.imgDirt.PopupMenu = self.cm
It's basically split into three parts.
PopupMenu
component.MenuItem
components, give them a Caption (Text
), and set their Parent
to the PopupMenu
component.PopupMenu
to whatever component you want it to appear on. I assigned it to my Image
component.