Python core programming framework

Source: Internet
Author: User

The following article mainly introduces the construction and skills of Python core programming. Python is indeed a very brilliant and powerful language that can run and operate programs on different platforms, even for users who do not know development and programming, Python is indeed a good language to start.

So put them in this code first, and some questions will be raised later. The second line is interesting. Import the wxPython wx library. Of course, the basic wxPython Declaration of the wx Library) includes basic classes, such as frameworks and applications.

/Note that there is no semicolon at the end of these rows. If you have compiled a Perl program, you need to familiarize yourself with the input habits of Python. But wait, it is unfamiliar to you. In the third row, the first Python class-main_window is defined. The main_window class is the derived class of the wxFrame class defined in the wx module. As you guessed, any window is a wxFrame class.

In the fourth row, the initialization method is defined. The only method that main_window needs to be defined is, of course, other methods are in the wxFrame class ). The initialization method takes the parameter parent, id, title, and, of course, reference to this object) self. The self parameter is the first parameter in all core Python programming methods.

Until now, if you have written C/C ++, you must be wondering why there are no curly braces. It is strange that Python regards indentation as an important syntax. Any statement with a sub-statement ends with a colon. All the contraction operations in this row belong to the statement terminated with this colon.

When the indent is restored to the original indent, this is done. What's even more strange is that this kind of arrangement is actually very useful. programmers who just started programming found that it is easier to describe the structure with curly brackets. Fortunately, this also means less keys, so the old programmer can accept it.

Therefore, the indentation of rows 5th, 6, and 7 belongs to the method defined in rows 4th. They call the initialization routine of the wxFrame parent class to actually set all the heavy work of a window), define a control to fit the new main window, and ensure that the window is visible.

WxPython will re-adjust the size of the parent window, unless specifically told not to do so. If you have written any Microsoft Windows code and re-adjusted the control size to match the parent window, you will soon like wxPython.

The actual significance of Line 2 is to complete an editor like Notepad. This is nothing. Using C/C ++ code requires more to be implemented, isn't it? Remember-what we do here is not a fixed-size dialog box. It is a Windows-based application with an editor that can be adjusted in the window body.

Let's continue. Row 3 defines the App object derived from the wxApp class. It specifies the application object. When running, it creates a main_window object and sets it as the top window. After defining the class, create an application object and start running its main loop.

If you have done any C/C ++ Windows programming, you will realize that the MainLoop method is a normal event loop of all Windows programs. The startup code style is indeed consistent with that of the script language. Remember, the Python interpreter reads the code line by line and executes it while reading it. Therefore, once a class is defined, we only need to call it in the script.

This is it. In these 15 lines of code, a simple text editor is implemented, which can be run on Windows or UNIX without any modifications. It is easy to add more features, which will be described in the next section.

You may think that "interpreted language is less efficient. For large programs, the execution will be slow ." To some extent, this is correct. In fact, any code that affects performance is usually re-implemented in C/C ++ and linked to the Python core programming, which is easy to do. Therefore, Python is usually used as an adhesive for binding functional modules and GUI display or application server functions, if you need this function. But as an adhesive, Python is very effective.

You can use Python to implement real programs in a short period of time. Due to limited opportunities in object-oriented and format creation, they can usually be used for several weeks. In addition, if you suspect that you want to use interpreted languages to combine large programs, consider how Microsoft Word's earlier versions are implemented.

At least in the latest Windows version of Word 6.0, the Word Basic function is actually only compiled code, and the GUI is built by the pcode explanatory language. MS Word 6.0 is written in Word Basic and effective, which is a clever design, one of the reasons -- it was the earliest desktop program with a built-in interpreter .)

What you lose here is actually very little in terms of performance), but you can easily make up for it by simplifying implementation and more importantly) Simplifying customization. In fact, by including Python, you have automatically included a scripting language that is easy to present to your users. At the same time, this language also proves easy to learn for new programmers.

If you study it, you can create world-class software with less effort than you do now. If the language can serve Microsoft, it can also serve you! In any case, let's get down to the truth. Here we will show you a program that actually does something worth doing. This program allows you to create a text file list called a project. You can edit and save them.

More importantly, you can easily see how to further enhance the basic organizer. I am using a front-end with an extended version of the CVS standard Open Source version control system. The code is below. We are still quite small) the application expands from 15 lines to about 300 lines, but it can now implement many things.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.