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)