简体   繁体   English

从wx Frame类调用应用程序方法

[英]Calling Application Methods from a wx Frame Class

I'm starting out with wxPython and have been working my way through every tutorial and example I can get my hands on. 我从wxPython开始,并一直在逐步完成每本教程和示例。 I've run into a slight problem, however, and it has to do with the wx.App versus the wx.Frame and which should contain specific methods. 但是,我遇到了一个小问题,它与wx.App和wx.Frame有关,并且应该包含特定的方法。 Just about every example I've seen don't go much beyond layouts/sizers and event handling, none really tackle project organization of a wxPython project. 我所看到的几乎每个示例都没有超出布局/大小和事件处理的范围,没有一个可以真正解决wxPython项目的项目组织问题。

For example, I have a method that gets a list of folders. 例如,我有一个获取文件夹列表的方法。 The way most examples would deal with this would be to stick the method right in the frame class. 大多数示例处理此问题的方法是将方法正确地保留在框架类中。 This method has the potential to be used in several other parts of the application, so it would make more sense to store it at the application class level. 此方法有可能在应用程序的其他几个部分中使用,因此将其存储在应用程序类级别会更有意义。

How should I organize and call "universal" methods like these so that I don't clutter up my frame classes. 我应该如何组织和调用这样的“通用”方法,以免使框架类混乱。

UPDATE: 更新:

To clarify, the "list of folders" was just an example, my actual method does a lot more work. 要澄清的是,“文件夹列表”只是一个例子,我的实际方法做了很多工作。 What I'm saying is I have code that isn't Frame-specific. 我的意思是说我有不是特定于框架的代码。 If I had this in the application class, what is the best way to call it from and event method in my frame. 如果我在应用程序类中有此方法,那么在我的框架中从and方法调用它的最佳方法是什么。

I'm looking for actual project organization techniques, not programming fundamentals. 我在寻找实际的项目组织技术,而不是编程基础知识。

As Mark stated you should make a new class that handles things like this. 正如Mark所言,您应该创建一个新类来处理此类事情。

The ideal layout of code when using something like wxWidgets is the model view controller where the wxFrame class only has the code needed to display items and all the logic and business rules are handled by other class that interact with the wxFrame. 使用wxWidgets之类的代码时,理想的代码布局是模型视图控制器,其中wxFrame类仅具有显示项目所需的代码,并且所有逻辑和业务规则由与wxFrame交互的其他类处理。 This way you can change logic and business rules with out having to change your interface and change (or swap) your interface with out having to change your logic and business rules. 这样,您无需更改逻辑和业务规则即可更改逻辑和业务规则,而无需更改逻辑和业务规则即可更改(或交换)接口。

Your classes that inherit from wxWidgets/wxPython data types should not implement any business logic. 从wxWidgets / wxPython数据类型继承的类不应实现任何业务逻辑。 wxWidgets is a GUI library, so any subclasses of wxApp or wxFrame should remain focused on GUI, that is on displaying the interface and being responsive to user actions. wxWidgets是一个GUI库,因此wxApp或wxFrame的任何子类都应专注于GUI,即显示界面并响应用户操作。

The code that does something useful should be separated from wx, as you can decide later to use it in some web or console application and you don't want to create wxApp object in such case. 有用的代码应与wx分开,因为您以后可以决定在某些Web或控制台应用程序中使用它,并且您不想在这种情况下创建wxApp对象。 You can also decide later on to move some computations to separate 'worker threads', while your GUI will be the 'main thread' - responsive, and repainted properly during long lasting computations. 您还可以稍后决定将一些计算移至单独的“工作线程”,而您的GUI将成为“主线程”-响应良好,并在长期计算期间正确地重新绘制。

Last but not least - the classes that encapsulate your logic might tend to grow during projects lifetime. 最后但并非最不重要的一点-封装逻辑的类在项目生命周期中可能会增长。 If they're mixed with your GUI classes they will grow faster, and finally they become so complex that you're almost unable to debug them... 如果它们与您的GUI类混合在一起,它们将增长得更快,最后它们变得如此复杂,以至于您几乎无法对其进行调试...

While having them separated leads to clean code when you don't mix bugs in logic with bugs in GUI (refreshing/layout/progress bar etc.). 当您将逻辑上的错误与GUI中的错误(刷新/布局/进度条等)混合使用时,将它们分开会导致干净的代码。 Such approach has another nice feature - ability to split work among GUI-people and logic-people, which can do their work without constant conflicts. 这种方法还有一个不错的功能-能够在GUI人员和逻辑人员之间拆分工作,从而可以在没有持续冲突的情况下完成工作。

I probably should have been a lot clearer from the start, but I found what I was looking for: 从一开始我可能应该更加清楚,但是我发现了我想要的东西:

http://wiki.wxpython.org/ModelViewController/ http://wiki.wxpython.org/ModelViewController/

Burried within the wxpython wiki, I found several simple, concrete examples of MVC projects. 在wxpython Wiki中,我发现了几个简单的MVC项目的具体示例。

在适当的OOP设计中,这将是独立的或文件系统类的一部分-它将不属于应用程序或框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM