Pyqt mining mine Game Study Notes (3)

Source: Internet
Author: User

This program uses Python's distinctive syntax routines. First.

The class mainwindow function Init () in Main. py has the following statement:

 
Self. Scene. setmap (Conf. W, Conf. h), Conf. Mines)

The conf variable is defined in config. py and introduced by the following statement:

 
From config import *

What are Conf. W and Conf. h here? Therefore, transfer to config. py to find out.

# Module: configimport JSON ults = {'splash ': false, 'w': 10, 'H': 10, 'mines': 1, 'scores': [],} default_save_file = "config. cfg "class _ conf: def _ getattr _ (self, name): Return defaults [name] def _ setattr _ (self, name, value ): if type (value )! = Type (defaults [name]): Raise exception ("% S % s is not % s:" % (value, type (value ), type (defaults [name]) defaults [name] = value def save (self, filename = default_save_file): With open (filename, 'W + ') as FP: JSON. dump (defaults, FP, indent = true) def load (self, filename = default_save_file): With open (filename, 'R') as FP: global defaults = JSON. load (FP) conf = _ conf () Try: Conf. load () handle T: Pass

As you can see, importing main. py to this module may cause the following:

Create and initialize a module-level variable. The defaults dictionary is used;

Create and initialize a module-level variable, default_save_file = "config. cfg"

Create a module-level variable conf and use this variable to instantiate _ conf;

Use conf to reference the function load () in _ conf and read the data in the config. cfg file into defaults.

There are no member variables W and H in the _ Conf, but why use Conf. W and Conf. h?

The secret lies in the special function _ getattr _ (name) in _ Conf)

When reading data with Conf. W, the value of the parameter name of __getattr _ is "W ",

The returned value is defaults ["W"]. The value is equal to 10.

This syntax routine of Python reads the defaults operation of the dictionary and forms a member accessing the class.

Pyqt mining mine Game Study Notes (3)

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.