With WXPython when is it necessary to use classes?
I'm very new to python and i have some stupid question, thank you for helping!
wh开发者_如何学Cen I search online about wxpython questions, I see almost 100% of the people use class for wxpython. From the book i learnt, the author didn't mention using class for wxpython, instead he gave us a few examples that didn't use class.
I just want to know, when is it good to use class and when is it really not so necessary.
You will need to use a class to overwrite the behavior of an existing Widget.
Many tutorials online start by using a custom extension of wx.Frame
as the base window for the application, but in fact there is no real reason to do that.
If you are satisfied with everything that widgets normally provide out of the box there is no reason to use classes - in fact you should avoid Object Oriented Programming as much as possible. Using a more functional approach will result in cleaner code and less troubles.
In wxPython, you should use classes whenever you want to extend the functionality of something wxPython provides to you.
A lot of wxPython apps at the very least define a class inheriting from wx.Frame
to serve as the main GUI. If you need to add special behavior to certain widgets (e.g. add a method to wx.Button
), you might also want to write a class for that. Lastly, some special features in wxPython (like tabbed GUIs or wizards) require you to write a class for each page.
There's a lot of cases where you need to write a class. You almost always need to at least write one class for your main frame. Other than that, it depends on what kind of problems you're trying to solve.
I'm not sure what book you read, but if you haven't already I'd recommend wxPython in Action, which is really the definitive guide to wxPython.
精彩评论