]> git.saurik.com Git - wxWidgets.git/blame - wxPython/docs/wxPackage.txt
Don't allow parameterless wxDropSource
[wxWidgets.git] / wxPython / docs / wxPackage.txt
CommitLineData
1fded56b
RD
1=========================
2 The wxPython wx Package
3=========================
4
5--------------------------------------------------
6 Or, how to survive the new wx namespace changes.
7--------------------------------------------------
8
9:Author: Patrick K. O'Brien
10:Contact: pobrien@orbtech.com
11:Organization: Orbtech_
12:Date: $Date$
13:Revision: $Revision$
14
15.. _Orbtech: http://www.orbtech.com/
16
17.. contents::
18
19
20Introduction
21============
22
23Big things sometimes come in small packages. This is certainly true
24of the new wx package, which is being introduced in wxPython 2.4.1 as
25a way to allow the "wx" prefix to be dropped from the names of all
26wxPython classes, functions, and constants. This document should
27answer all the questions you might have concerning the new wx package.
28If not, feel free to contact the author. I hope you like the new wx
29package as much as I do.
30
31
32Why change anything?
33====================
34
35This change is being made for a couple of reasons. The first reason
36is to discourage the use of ``import *``, which is a dangerous
37technique that can create name conflicts and bloated namespaces.
38
39The second reason is to remove what some perceive to be a "wart." For
40example, the following code is rather ugly in that the "wx" prefix on
41the wxFrame class name is no longer useful when you're using the wx
42module prefix::
43
44 from wxPython import wx
45
46 class Frame(wx.wxFrame)
47
48The new wx package allows you to write code like this, instead::
49
50 import wx
51
52 class Frame(wx.Frame)
53
54The third reason is that the wxWindows project intends to do the same
55thing (implement a new wx namespace and drop the "wx" prefix) and we
56want wxPython to lead the way.
57
58
59What does the new wx package do?
60================================
61
62As a way of getting to this new syntax as quickly as possible, the
63code in this new wx package was created. What it does is alter the
64existing wx namespace dynamically. By making the changes on-the-fly
65at runtime, we can try out the new syntax before any permanent changes
66are made to the underlying class library. The downside of making
67these changes at runtime is that there is a slight delay when you
68``import wx``; the upside is that you can start using the new syntax
69now.
70
71
72Will any of this effect my existing code?
73=========================================
74
75No. Your existing code will continue to work and be supported for
76some time. It will be up to you to decide when to switch to the new
77syntax. But all new documentation and code examples will use the new
78syntax. So don't wait too long. You wouldn't want anyone calling you
79old-fashioned, would you?
80
81
82How does the new wx package work?
83=================================
84
85It's pretty simple, and pretty clever. The wx directory contains an
86``__init__.py`` file, making it a Python package. (In contrast, the
87old wxPython.wx module is a module, not a package.) When you ``import
88wx`` the code in the ``__init__.py`` file is executed, and that's
89where all the magic takes place. Let's take a look at the code inside
90the ``__init__.py`` file:
91
92.. include:: ../wx/__init__.py
93 :literal:
94
95Namespaces in Python are implemented as dictionaries. The dictionary
96used to create the wx package's namespace is accessible using the
97``globals()`` function. The dictionary used to create the old
98wxPython.wx module's namespace is ``wx.__dict__``. Once we have these
99two dictionaries, it's a simple matter of iterating through one,
100changing the names, adding the renamed object to the other dictionary,
101and cleaning up a few local variables and imported modules. Voila!
102
103
104What about all the other modules, like grid, html, and stc?
105===========================================================
106
107There's more to wxPython than just the wx namespace. And we've got
108those extra modules covered as well. For each of those modules (as
109well as the lib package) we've got matching modules in the new wx
110package. Let's take a look at a few of them.
111
112Here is ``html.py``:
113
114.. include:: ../wx/html.py
115 :literal:
116
117And here is ``lib/dialogs.py``:
118
119.. include:: ../wx/lib/dialogs.py
120 :literal:
121
122As you can see, they both rely on the ``prefix.rename()`` function
123defined in ``prefix.py``:
124
125.. include:: ../wx/prefix.py
126 :literal:
127
128Again, the technique is very similar to the one used by the wx
129package.
130
131
132How do I use this new wx package?
133=================================
134
135The wx package is automatically created when you install wxPython
136version 2.4.1 or higher. So all you have to do is::
137
138 import wx
139
140
141What are the issues with converting old code to use the new wx package?
142=======================================================================
143
144Obviously, you need to change your import statements from::
145
146 from wxPython import wx
147
148or::
149
150 from wxPython.wx import *
151
152to::
153
154 import wx
155
156Then you need to refer to wx attributes without a "wx" prefix, such
157as::
158
159 class MyFrame(wx.Frame):
160
161In most cases, existing code can be modified with a simple search and
162replace.
163
164One extra issue you might run into when converting existing code is
165that the wx.__version__ attribute is no longer available, since the
166new wx namespace doesn't include any private attributes from the old
167wxPython.wx namespace. The solution is to use the wx.VERSION_STRING
168attribute, which was introduced in wxPython 2.4.1.
169
170
171Where can I find example programs using the new wx syntax?
172==========================================================
173
174Example programs are included in the wxPython/samples/wx_examples
175directory, and are documented in the wxPythonExamples_ documentation
176file. Also, all the code in the py package uses the new wx syntax.
177You can learn more about these in the PyManual_.
178
179.. _wxPythonExamples: wxPythonExamples.html
180.. _PyManual: PyManual.html