Windows in Python create a class

Source: Internet
Author: User
Tags constructor in python

The

has already learned how to encapsulate the functions of the Registration window in the form of a class, followed by the ability to create a window, and how this part of the functionality is most appropriate, so that all subsequent development work can reuse the code. Let's take a closer look at what the main thing on the window is mutable and what remains unchanged, in principle, is to change the variable into something that is changed in a functional way, and the unchanging thing is placed inside the class, thus reducing the problem that the class user is considering in detail. At present, a window is the easiest to change the two things: the window type, indicating that the window belongs to that kind of, such as edit, list, and so on; the other is the title of the window, because the user of the software can only distinguish the same type of window from the title of the window. In fact, these two points is from different angles to distinguish the needs of the window, window type is from the developer perspective to see the types of different windows; The title of the window is to look at the functional distinctions of different windows from the user's point of view. With these two points of thought, you can write the code for this class, the complete code is as follows:

 #开发人员: Cai Junsheng (qq:9073204) Shenzhen 2014-8-20 #创建窗口 class Winmaker:def __init__ (self, ClassName, hinst): Self.classname = ClassName Self.hinst = hinst Self.style = Ws_overlappedwindow | Ws_caption Self.exstyle = 0 self.x = Cw_usedefault self.y = 0 Self.width = Cw_usedefau LT self.height = 0 self.hwndparent = hwnd (0) Self.hmenu = hwnd (0) Self.wndcreatdata = C_void_p (0) def Create (self, title): Self.hwnd = Windll.user32.CreateWindowExW (self.exsty  
            Le, Self.classname, title, Self.style, self.x, Self.y, Self.width, Self.height,  
          
        Self.hwndparent, Self.hmenu, Self.hinst, Self.wndcreatdata) If not Self.hWnd:print ("Failed to create Window") exit (0) return s Elf.hwnd 

Class is the keyword of the Python class, which is used primarily to define the class name. Winmaker is the name of the class that I named creating the window, and you can use this class later to create many many windows without having to copy and paste n more code. Then you see the __init__ constructor, the main thing in this constructor is to create all the required parameters for the window. In the argument list for this constructor, ClassName is the name of the window type; Hinst is the handle that the window belongs to the application instance. When created, other window parameters use the default parameters. Of course, if you want to control more window properties, you can add it to the argument list. At last, we define the creation function to create the operation of the window, in which the title of the window is passed through the parameter, so that the window can display different names. In this code, the main use of Python's class three features, one is the Python class constructor __init__; one is the Python class member function create; one is a member variable of the Python class, such as Self.classname = ClassName.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Related Article

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.