Deprecation warnings are useful methods for open-source maintainers to alert their customers that some a part of their package deal is now not going to be supported. Good maintainers will present migration guides that let you know what to do if you see a deprecation warning.
You don’t need to ignore a deprecation warning as a result of it alerts you {that a} function is disappearing. When you improve to the most recent with out mitigating the warnings, you’ll virtually definitely have a number of damaged functions to repair.
wxPython shouldn’t be resistant to deprecation warnings both. On this article, you’ll study what to do when see a DeprecationWarning about wx.NewId().
Ye Olde wx.NewId
If you want to see the deprecation error, all you want is wxPython 4-4.2 or so and add a name to wx.NewId().
Probably the most frequent locations to make use of wx.NewId() is in a wx.Menu merchandise. Right here’s an instance:
import wx class MyFrame(wx.Body): def __init__(self): tremendous().__init__(father or mother=None, title="wx.Menu Tutorial") self.panel = wx.Panel(self, wx.ID_ANY) menu_bar = wx.MenuBar() file_menu = wx.Menu() exit_menu_item = file_menu.Append(wx.NewId(), "Exit", "Exit the appliance") menu_bar.Append(file_menu, "&File") self.Bind(wx.EVT_MENU, self.on_exit, exit_menu_item) self.SetMenuBar(menu_bar) def on_exit(self, occasion): self.Shut() # Run this system if __name__ == "__main__": app = wx.App(False) body = MyFrame().Present() app.MainLoop()
Whenever you run this code, you will note the next textual content in your terminal or IDE’s stdout:
C:codebad_menu.py:12: DeprecationWarning: NewId() is deprecated exit_menu_item = file_menu.Append(wx.NewId(), "Exit", "Exit the appliance")
Let’s learn the way to repair this drawback within the subsequent part!
Utilizing wx.NewIdRef()
The repair for this explicit deprecation warning from wxPython is to make use of wx.NewIdRef() as an alternative of wx.NewId().
Swap out the brand new name like so:
import wx class MyFrame(wx.Body): def __init__(self): tremendous().__init__(father or mother=None, title="wx.Menu Tutorial") self.panel = wx.Panel(self, wx.ID_ANY) menu_bar = wx.MenuBar() file_menu = wx.Menu() exit_menu_item = file_menu.Append(wx.NewIdRef(), "Exit", "Exit the appliance") menu_bar.Append(file_menu, "&File") self.Bind(wx.EVT_MENU, self.on_exit, exit_menu_item) self.SetMenuBar(menu_bar) def on_exit(self, occasion): self.Shut() # Run this system if __name__ == "__main__": app = wx.App(False) body = MyFrame().Present() app.MainLoop()
You’ll now not obtain a deprecation warning if you run this up to date code!
Wrapping Up
Deprecation warnings aren’t a giant deal. However for those who plan to do a brand new launch, then you must spend a bit additional time caring for them in order that they don’t chunk you in a while.
Have enjoyable and glad coding!