"""Switchboard class.This class is used to coordinate updates among all Viewers. Every Viewer mustconform to the following interface:- it must include a method called update_yourself() which takes threearguments; the red, green, and blue values of the selected color.- When a Viewer selects a color and wishes to update all other Views, itshould call update_views() on the Switchboard object. Note that theViewer typically does *not* update itself before calling update_views(),since this would cause it to get updated twice.Optionally, Viewers can also implement:- save_options() which takes an optiondb (a dictionary). Store into thisdictionary any values the Viewer wants to save in the persistent~/.pynche file. This dictionary is saved using marshal. The namespacefor the keys is ad-hoc; make sure you don't clobber some other Viewer'skeys!- withdraw() which takes no arguments. This is called when Pynche isunmapped. All Viewers should implement this.- colordb_changed() which takes a single argument, an instance ofColorDB. This is called whenever the color name database is changed andgives a chance for the Viewers to do something on those events. SeeListViewer for details.External Viewers are found dynamically. Viewer modules should have names suchas FooViewer.py. If such a named module has a module global variable calledADDTOVIEW and this variable is true, the Viewer will be added dynamically tothe `View' menu. ADDTOVIEW contains a string which is used as the menu itemto display the Viewer (one kludge: if the string contains a `%', this is usedto indicate that the next character will get an underline in the menu,otherwise the first character is underlined).FooViewer.py should contain a class called FooViewer, and its constructorshould take two arguments, an instance of Switchboard, and optionally a Tkmaster window."""import sysimport marshalclass Switchboard:def __init__(self, initfile):self.__initfile = initfileself.__colordb = Noneself.__optiondb = {}self.__views = []self.__red = 0self.__green = 0self.__blue = 0self.__canceled = 0# read the initialization filefp = Noneif initfile:try:try:fp = open(initfile, 'rb')self.__optiondb = marshal.load(fp)if not isinstance(self.__optiondb, dict):print('Problem reading options from file:', initfile,file=sys.stderr)self.__optiondb = {}except (IOError, EOFError, ValueError):passfinally:if fp:fp.close()def add_view(self, view):self.__views.append(view)def update_views(self, red, green, blue):self.__red = redself.__green = greenself.__blue = bluefor v in self.__views:v.update_yourself(red, green, blue)def update_views_current(self):self.update_views(self.__red, self.__green, self.__blue)def current_rgb(self):return self.__red, self.__green, self.__bluedef colordb(self):return self.__colordbdef set_colordb(self, colordb):self.__colordb = colordbfor v in self.__views:if hasattr(v, 'colordb_changed'):v.colordb_changed(colordb)self.update_views_current()def optiondb(self):return self.__optiondbdef save_views(self):# save the current colorself.__optiondb['RED'] = self.__redself.__optiondb['GREEN'] = self.__greenself.__optiondb['BLUE'] = self.__bluefor v in self.__views:if hasattr(v, 'save_options'):v.save_options(self.__optiondb)# save the name of the file used for the color database. we'll try to# load this first.self.__optiondb['DBFILE'] = self.__colordb.filename()fp = Nonetry:try:fp = open(self.__initfile, 'wb')except IOError:print('Cannot write options to file:', \self.__initfile, file=sys.stderr)else:marshal.dump(self.__optiondb, fp)finally:if fp:fp.close()def withdraw_views(self):for v in self.__views:if hasattr(v, 'withdraw'):v.withdraw()def canceled(self, flag=1):self.__canceled = flagdef canceled_p(self):return self.__canceled
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。