簡體   English   中英

如何讓菜單選項在 WxPython 中做不同的事情?

[英]How do I make menu options do different things in WxPython?

我在 WxPython 中的菜單有問題,在文件 header 中,我有“文件”,並且有 2 個選項。 '保存'和'關閉'..,當我點擊保存時,我希望它打印“已保存”並且當我時鍾退出時。 應用程序應該打印“已關閉”並關閉...但是如果我單擊甚至保存它正在關閉:所以請您幫幫我。 這是我的代碼的一部分:

menubar = wx.MenuBar()
fileMenu = wx.Menu()

menubar.Append ( fileMenu, '&File' )

m1 = fileMenu.Append ( wx.ID_EXIT, 'Save' )
self.Bind ( wx.EVT_MENU, self.OnSave, m1 )

m2 = fileMenu.Append ( wx.ID_EXIT, 'Quit' )
self.Bind ( wx.EVT_MENU, self.OnQuit, m2 )

self.SetMenuBar ( menubar )

以及功能:

def OnSave ( self, event ):
    
    text = self.text_ctrl.GetValue()
    
    fil.write ( text )
    print ( "Saved file")

def OnQuit ( self, event ):
    
    print ( "Closed" )
    self.Close()

fileMenu.Append的第一個參數很重要。 它用於將一個菜單項與另一個菜單項區分開來,但您對兩者使用了相同的值。

    m1 = fileMenu.Append(wx.ID_SAVE, 'Save')
    self.Bind(wx.EVT_MENU, self.OnSave, m1)
    
    m2 = fileMenu.Append(wx.ID_EXIT, 'Quit')
    self.Bind(wx.EVT_MENU, self.OnQuit, m2)

在我的程序中,我更喜歡使用 wx.NewId() 從系統獲取免費 ID。 如果您有這些“庫存”菜單條目,則 ID_SAVE 和 ID_EXIT 非常有意義,但如果您自己輸入條目,則可以執行以下操作:

    m3 = fileMenu.Append(wx.NewId(), 'My own menu item')
    self.Bind(wx.EVT_MENU, self.OnMyOwnFunction, m3)
    m4 = fileMenu.Append (wx.NewId(), 'Another menu item')
    self.Bind(wx.EVT_MENU, self.OnAnotherFunction, m4)

您對兩個菜單項使用相同的Id ,即wx.ID_EXIT ,正如@Petr Blahos 指出的那樣,這會導致您的問題。
然而,Petr 的回答存在問題,雖然在技術上是正確的,
wx.NewId現在Deprecated ,盡管它仍然有效,但已被wx.NewIdRef取代。

wx.NewId()
Generates an integer identifier unique to this run of the program.

Return type
int

Deprecated IDs generated by this function can possibly conflict with IDs used elsewhere in the application code. It is recommended to instead use the wx.ID_ANY ID to assign generated IDs for the controls, menu items and etc. that you create in the application. These IDs are guaranteed to not conflict with the other IDs that are in use in the application. For those cases where you need to create an ID that can be used more than once then please see wx.NewIdRef.

wx.NewIdRef(count=1)
Reserves a new Window ID (or range of WindowIDs) and returns a wx.WindowIDRef object (or list of them) that will help manage the reservation of that ID.

This function is intended to be a drop-in replacement of the old and deprecated wx.NewId function, with the added benefit that the ID should never conflict with an in-use ID or other IDs generated by this function.

wx.NewIdRef也有一個count function,所以它可以用來grab一組你根據需要使用的 id。 IE

>>> myIds = wx.NewIdRef(count=6)
>>> myIds
[WindowIDRef: -31973, WindowIDRef: -31972, WindowIDRef: -31971, WindowIDRef: -31970, WindowIDRef: -31969, WindowIDRef: -31968]
>>> useId = myIds[3].Id
>>> useId
-31970

請注意wx.NewIdRefwx.ID_ANY-1都可以在此菜單上下文中使用。 看這個簡單的例子:

import wx

class Test(wx.Frame):

    def __init__(self,parent):
        wx.Frame.__init__(self,parent,title="Frame aka Window",size = (300,200))
        panel = wx.Panel(self)
        self.status=self.CreateStatusBar()
        self.status.SetStatusText("Status bar text")
        menubar=wx.MenuBar()
        firstm=wx.Menu()
        secondm=wx.Menu()

        fm1 = wx.MenuItem(firstm, wx.NewIdRef(), 'New Window\tAlt+N')
        firstm.Append(fm1)
        self.Bind(wx.EVT_MENU, self.OnMenu1, id=fm1.GetId())
        fm2 = wx.MenuItem(firstm, wx.NewIdRef(), 'Open', "Text for the statusbar")
        firstm.Append(fm2)
        self.Bind(wx.EVT_MENU, self.OnMenu2, id=fm2.GetId())
        fm3 = wx.MenuItem(firstm, -1, 'Quit\tAlt+Q')
        firstm.Append(fm3)
        self.Bind(wx.EVT_MENU, self.OnMenu3, id=fm3.GetId())

        sm1 = wx.MenuItem(firstm, wx.ID_ANY, 'Re-Do', "Statusbar Re-Do")
        secondm.Append(sm1)
        self.Bind(wx.EVT_MENU, self.OnsMenu1, id=sm1.GetId())
        sm2 = wx.MenuItem(secondm, wx.ID_ANY, 'Un-Do', "Statusbar Un-Do")
        secondm.Append(sm2)
        self.Bind(wx.EVT_MENU, self.OnsMenu2, id=sm2.GetId())

        menubar.Append(firstm,"File")
        menubar.Append(secondm,"Edit")

        self.SetMenuBar(menubar)

        t = wx.StaticText(panel,-1,"Hello i'm a test", pos=(10,20))

    def OnMenu1(self, event):
        print("Menu item 1",event.GetId())

    def OnMenu2(self, event):
        print("Menu item 2",event.GetId())
        
    def OnMenu3(self, event):
        print("Menu item 3 Quit",event.GetId())
        self.Destroy()
                
    def OnsMenu1(self, event):
        print("2nd Menu item 1",event.GetId())

    def OnsMenu2(self, event):
        print("2nd Menu item 2",event.GetId())

if __name__=='__main__':
    app=wx.App()
    frame=Test(None)
    frame.Show()
    app.MainLoop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM