Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

menu - How to change colours on wx FlatMenu

I'm trying to create a popup menu which look could be completely customized. Since wx.Menu is based on native widget, I tried tu use FlatMenu, but even though the documentation states that such customization is possible, I failed to find out how to do it (except for the menu item text color). Could someone show me how to change background and border colours for FlatMenu on the following code (I don't use MenuBar, since I want to create a pop-up menu) ? Or, if I was wrong to believe it is possible with FlatMenu, can someone point me out another module that allows this?

import wx

from wx.lib.agw.flatmenu import FlatMenu as FlatMenu, FlatMenuItem as FlatMenuItem
class MyFrame(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, parent, -1, "FlatMenu Demo")

        panel = wx.Panel(self, -1)
        self.btn = wx.Button(panel, -1, "Hello", (15, 12), (100, 120))
        self.btn.Bind(wx.EVT_BUTTON, self.pop)

        self.menubar = FlatMenuBar(self, -1)
        self.orderMenu = FlatMenu(panel)
        self.menubar.SetBackgroundColour(wx.RED)
         
        item_title = FlatMenuItem(self.orderMenu, 1, "Title",
                               "Sort by movie title", wx.ITEM_RADIO, None)
        self.Bind(wx.EVT_MENU, self.selected, id=1)
        self.orderMenu.AppendItem(item_title)
        self.Bind(wx.EVT_MENU, self.selected, item_title)
        
        item_year = FlatMenuItem(self.orderMenu, 2, "Year",
                               "Sort by year", wx.ITEM_RADIO, None)
        self.Bind(wx.EVT_MENU, self.selected, id=1)
        self.orderMenu.AppendItem(item_year)
        
        self.Bind(wx.EVT_MENU, self.selected, item_year)
        
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(panel, 1, wx.EXPAND)

        self.SetSizer(main_sizer)
        main_sizer.Layout()


    def selected(self, event):
        print(event)
    
    
    def pop(self, event):
        print("pop")
        x, y = self.btn.GetScreenPosition()
        y = y+self.btn.GetSize()[1]
        self.orderMenu.Popup(wx.Point(x, y), self)

# our normal wxApp-derived class, as usual

app = wx.App(0)

frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()

app.MainLoop()
question from:https://stackoverflow.com/questions/65888231/how-to-change-colours-on-wx-flatmenu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...