pax_global_header00006660000000000000000000000064141664530240014517gustar00rootroot0000000000000052 comment=83ec14a4dbc8d30c82c349ecae8a0176784137f9 bkchem-0.14.0~pre4+git20211228/000077500000000000000000000000001416645302400154245ustar00rootroot00000000000000bkchem-0.14.0~pre4+git20211228/BUGS000066400000000000000000000001221416645302400161020ustar00rootroot000000000000007. -arrow-heads in svg export are not perfect 8. -fonts in postscript are problembkchem-0.14.0~pre4+git20211228/INSTALL000066400000000000000000000051561416645302400164640ustar00rootroot00000000000000Note: This file describes the installation of source release of BKChem. If you have downloaded the binary release, please read the INSTALL.binary file instead. Installation of BKChem is very simple. But before you can run the program you must install the following: 1/ Python. On www.python.org get a distribution of Python for your platform. Note that BKChem will run only on Python 2.6 or newer. 2/ pycairo This library is optional and is available only for Linux. It enables high quality export to PDF and PNG formats with support for antialiased unicode texts. It requires the cairo library. Both can be found on http://cairographics.org/snapshots/. The current and tested version is 0.5.1. --- Single directory deployment --- No installation is needed to run BKChem. This means that you can run the program directly after unpacking the downloaded sources. Simply run the bkchem.py located in the 'bkchem-X.Y.Z/bkchem' directory, where X,Y,Z are version numbers of BKChem release. example: on Unix do >cd "the dir where you have downloaded the BKChem package" >tar -xzf bkchem-X.X.X.tgz -C "the dir where you want to unpack BKChem" >cd "the dir where you have unpacked BKChem"/bkchem-X.Y.Z/bkchem >python bkchem.py --- System-wide install --- Under Linux and other Unix systems you have the possibility to use a 'classic' way of installing software. Major advantage of doing this is that it you will have a "bkchem" program in your path, thus easily startable from any path. For the purpose of installation BKChem uses a standard for installation of python modules - distutils. The installation is accomplished by simply running the setup.py script from the bkchem directory with 'install' argument: >python setup.py install NOTE: usually you have to be root in order to be able to perform the install! This way all python sources will be installed in a standard directory for third-party modules - usually something like '/usr/lib/python/site-packages'. The templates, pixmaps and other such stuff will go into 'prefix/share/bkchem', where prefix is usually 'usr' or 'usr/local'. Documentation will be put in 'prefix/share/doc/bkchem'. Finally a sh-script 'prefix/bin/bkchem' will be created so that you can run bkchem from anywhere you want. To influence the paths that are used during install - especially the 'prefix' run: >python setup.py install --help to see the available options. Running >python setup.py --help will give you another set of options available for the install. Any comments or reports on installation process are especially welcome as this is very hard to test thoroughly on a single machine. bkchem-0.14.0~pre4+git20211228/MANIFEST.in000066400000000000000000000006441416645302400171660ustar00rootroot00000000000000include INSTALL README progress.log include gpl.txt include templates/*.cdml include templates/*.xml include plugins/*.xml include plugins/*.py include pixmaps/*.gif include images/logo.ppm include images/icon.ico include images/bkchem.svg include images/bkchem*.png recursive-include doc *.html *.py *.png *.pdf recursive-include dtd *.dtd *.xsd recursive-include locale LC_MESSAGES/BKChem.mo include locale/BKChem.pot bkchem-0.14.0~pre4+git20211228/README000066400000000000000000000040331416645302400163040ustar00rootroot00000000000000BKChem is a free molecular drawing program. For more info on program capabilities, install instructions etc. see documentation in directory "doc" in the main BKChem directory or the BKChem website on http://zirael.org/bkchem/ Bug reports and suggestions please fill on Savanna: http://savannah.nongnu.org/projects/bkchem at GitLab issues or send to the current maintainer: Reinis Danne --- BKChem is free software and is distributed under GNU GPL. The program is provided as is without warranty of any kind. For details see the file "gpl.txt" in main bkchem directory. --- For installation instructions see the file INSTALL in main BKChem directory. --- NOTE: Starting with the version 0.9.0-pre1 Piddle sources are shipped together with BKChem. Piddle is a library provided under the GPL license and seems to be dead at least 2 years. Therefore it was decided to put it inside BKChem sources and distribute any eventual additions this way. In case any major changes will be made Piddle will be released as a standalone library. NOTE: Starting with the version 0.10.0-pre1 Pmw (http://pmw.sourceforge.net/) sources (version 1.2) are shipped together with BKChem. There were needed some minor fixes in Pwm and as it is a library provided under the GPL license and seems to be dead it was decided to put it inside bkchem sources and distribute the fixed version this way. NOTE: The official InChI software from IUPAC is bundled in a binary form together with BKChem in Windows binary builds. All the credit for this nice piece of software goes to its creators. This information comes with the software: Copyright © 2011 IUPAC and InChI Trust Limited International Chemical Identifier (InChI) International Union of Pure and Applied Chemistry (IUPAC) This library is free software; you can redistribute it and/or modify it under the terms of the IUPAC/InChI Trust InChI Licence No. 1.0, or (at your option) any later version. Contact ulrich@inchi-trust.org IUPAC http://www.iupac.org/inchi InChI Trust http://www.inchi-trust.org bkchem-0.14.0~pre4+git20211228/__init__.py000066400000000000000000000002401416645302400175310ustar00rootroot00000000000000 ## import sys ## sys.modules['bkchem'] = bkchem import sys import os sys.path.insert( 0, os.path.abspath('./bkchem/bkchem')) from bkchem import bkchem bkchem-0.14.0~pre4+git20211228/bkchem.iss000066400000000000000000000026621416645302400174030ustar00rootroot00000000000000; Script generated by the Inno Setup Script Wizard. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! [Setup] AppName=BKChem AppVerName=BKChem-0.14.0-pre4 OutputBaseFilename=bkchem-0.14.0-pre4 VersionInfoVersion=0.14.0.4 VersionInfoTextVersion=0.14.0-pre4 AppPublisher=Reinis Danne AppPublisherURL=http://bkchem.zirael.org AppSupportURL=http://bkchem.zirael.org AppUpdatesURL=http://bkchem.zirael.org/bkchem DefaultDirName={pf}\BKChem DefaultGroupName=BKChem AllowNoIcons=yes LicenseFile=gpl.txt Compression=lzma SolidCompression=yes SetupIconFile=images\icon.ico PrivilegesRequired=none [Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked [Files] Source: "*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] Name: "{group}\BKChem"; Filename: "{app}\bkchem\bkchem.exe"; WorkingDir: "{userdocs}"; IconFilename: "{app}\images\icon.ico" Name: "{group}\{cm:UninstallProgram,BKChem}"; Filename: "{uninstallexe}" Name: "{userdesktop}\BKChem"; Filename: "{app}\bkchem\bkchem.exe"; Tasks: desktopicon; WorkingDir: "{userdocs}"; IconFilename: "{app}\images\icon.ico" [Run] Filename: "{app}\bkchem\bkchem.exe"; Description: "{cm:LaunchProgram,BKChem}"; WorkingDir: "{userdocs}"; Flags: nowait postinstall skipifsilent [UninstallDelete] Type: Files; Name: "{app}\bkchem\tuning.pyc" bkchem-0.14.0~pre4+git20211228/bkchem/000077500000000000000000000000001416645302400166555ustar00rootroot00000000000000bkchem-0.14.0~pre4+git20211228/bkchem/CDML_versions.py000066400000000000000000000161051416645302400217010ustar00rootroot00000000000000#-------------------------------------------------------------------------- # This file is part of BKChem - a chemical drawing program # Copyright (C) 2002-2009 Beda Kosata # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # Complete text of GNU GPL can be found in the file gpl.txt in the # main directory of the program #-------------------------------------------------------------------------- """Support for backward compatible CDML reading. """ import data import dom_extensions as dom_ext class CDML_transformer_06_07(object): output_version = '0.7' input_version = '0.6' def tranform_dom( self, dom): for b in dom.getElementsByTagName( 'bond'): type = b.getAttribute( 'type') if type == 'forth': b.setAttribute( 'type', 'up') class CDML_transformer_07_08(object): output_version = '0.8' input_version = '0.7' def tranform_dom( self, dom): types = { 'single': 's', 'double': 'd', 'triple': 't', 'up': 'w', 'back': 'h'} for b in dom.getElementsByTagName( 'bond'): type = b.getAttribute( 'type') if type in types: t = types[ type] b.setAttribute( 'type', t) class CDML_transformer_08_09(object): output_version = '0.9' input_version = '0.8' def tranform_dom( self, dom): pass class CDML_transformer_09_10(object): output_version = '0.10' input_version = '0.9' def tranform_dom( self, dom): if dom.nodeName == 'cdml': cdml = dom else: cdml = dom.getElementsByTagName('cdml')[0] if cdml.getElementsByTagName( 'standard'): return standard = dom_ext.elementUnder( cdml, 'standard', (('font_family','helvetica'), ('font_size','12'), ('line_width','1.0px'))) dom_ext.elementUnder( standard, 'bond', (('double-ratio','1'), ('length','1.0cm'), ('width','6.0px'), ('wedge-width','2.0px'))) dom_ext.elementUnder( standard, 'arrow', (('length','1.6cm'),)) class CDML_transformer_10_11(object): output_version = '0.11' input_version = '0.10' bond_type_remap = ['','n1','n2','n3','w1','h1'] def tranform_dom( self, dom): for b in dom.getElementsByTagName("bond"): # bond remap type = b.getAttribute( 'type') if not type: continue elif type in data.alternative_bond_types: type = data.alternative_bond_types.index( type) elif type in data.bond_types: type = data.alternative_bond_types.index( type) else: try: type = int( type) except: continue type = self.bond_type_remap[ type] b.setAttribute('type', type) # other remaps # distance d = b.getAttribute( 'distance') if d: if type[0] == 'n': b.setAttribute( 'bond_width', d) else: d = float( d) # the drawing code was changed and divides by 2 now b.setAttribute( 'wedge_width', str(2*d)) b.removeAttribute( 'distance') # line_width w = b.getAttribute( 'width') if w: b.setAttribute( 'line_width', w) b.removeAttribute( 'width') class CDML_transformer_11_12(object): output_version = '0.12' input_version = '0.11' def tranform_dom( self, dom): pass class CDML_transformer_12_13(object): output_version = '0.13' input_version = '0.12' def tranform_dom( self, dom): for a in dom.getElementsByTagName( 'atom'): ch = a.getAttribute("charge") or 0 for m in a.getElementsByTagName( 'mark'): if m.getAttribute( 'type') == 'plus': ch += 1 elif m.getAttribute( 'type') == 'minus': ch -= 1 a.setAttribute( 'charge', str( ch)) class CDML_transformer_13_14(object): output_version = '0.14' input_version = '0.13' def tranform_dom( self, dom): for a in dom.getElementsByTagName( 'atom'): name = a.getAttribute( "name") if not name: a.tagName = "text" elif name in ('OCH3','NO2','COOH','COOCH3','Me','CN','SO3H','PPh3','OMe','Et','Ph','COCl','CH2OH'): a.tagName = "group" a.setAttribute( "group-type", "builtin") else: pass class CDML_transformer_14_15(object): """CDML version 0.15. Adds line_width attribute to the electronpair mark, also multiplicity is now stored explicitly. """ output_version = '0.15' input_version = '0.14' def tranform_dom( self, dom): for a in dom.getElementsByTagName( 'atom'): multiplicity = 1 for m in a.getElementsByTagName( 'mark'): if m.getAttribute( "type") == "electronpair": if not m.getAttribute( "line_width"): size = float( m.getAttribute( "size")) m.setAttribute( "line_width", str( round( round( size /2)/2))) if m.getAttribute( "type") == "radical": multiplicity += 1 elif m.getAttribute( "type") == "biradical": multiplicity += 2 if not a.hasAttribute("multiplicity"): a.setAttribute( "multiplicity", multiplicity) class CDML_transformer_15_16(object): """CDML version 0.16. Stores rich text as escaped value, not directly in XML - that is '<i>x</i>' instead of 'x'. """ output_version = '0.16' input_version = '0.15' def tranform_dom( self, dom): for a in dom.getElementsByTagName( 'ftext'): text = "" for ch in a.childNodes[:]: text += ch.toxml() a.removeChild( ch) a.appendChild( dom.ownerDocument.createTextNode( text)) # LIST OF AVAILABLE TRANSFORMERS transformers = { '0.6': CDML_transformer_06_07, '0.7': CDML_transformer_07_08, '0.8': CDML_transformer_08_09, '0.9': CDML_transformer_09_10, '0.10': CDML_transformer_10_11, '0.11': CDML_transformer_11_12, '0.12': CDML_transformer_12_13, '0.13': CDML_transformer_13_14, '0.14': CDML_transformer_14_15, '0.15': CDML_transformer_15_16, } # TRANSFORMING FUNCTION def transform_dom_to_version(dom, version): """Do inplace transformation of the dom tree to requested version. Return 1 on success. """ in_ver = dom.getAttribute('version') if not in_ver: return 0 out_ver = version trans = [] while in_ver != out_ver: if in_ver in transformers: trans.append( transformers[ in_ver]()) in_ver = trans[-1].output_version else: return 0 for tr in trans: tr.tranform_dom(dom) return 1 bkchem-0.14.0~pre4+git20211228/bkchem/Pmw.py000066400000000000000000012135761416645302400200110ustar00rootroot00000000000000import os import re import sys import time import string import traceback try: from collections.abc import Callable except ImportError: from collections import Callable try: import tkinter as Tkinter except ImportError: import Tkinter import PmwColor as Color from misc import myisstr as _myisstr # by BK to support non-ascii menus etc. if sys.version_info[0] == 2: str = unicode ### Loader functions: _VERSION = '1.2' def setversion(version): if version != _VERSION: raise ValueError('Dynamic versioning not available') def setalphaversions(*alpha_versions): if alpha_versions != (): raise ValueError('Dynamic versioning not available') def version(alpha=0): if alpha: return () else: return _VERSION def installedversions(alpha=0): if alpha: return () else: return (_VERSION,) ###################################################################### ### File: PmwBase.py # Pmw megawidget base classes. # This module provides a foundation for building megawidgets. It # contains the MegaArchetype class which manages component widgets and # configuration options. Also provided are the MegaToplevel and # MegaWidget classes, derived from the MegaArchetype class. The # MegaToplevel class contains a Tkinter Toplevel widget to act as the # container of the megawidget. This is used as the base class of all # megawidgets that are contained in their own top level window, such # as a Dialog window. The MegaWidget class contains a Tkinter Frame # to act as the container of the megawidget. This is used as the base # class of all other megawidgets, such as a ComboBox or ButtonBox. # # Megawidgets are built by creating a class that inherits from either # the MegaToplevel or MegaWidget class. # Special values used in index() methods of several megawidgets. END = ['end'] SELECT = ['select'] DEFAULT = ['default'] # Constant used to indicate that an option can only be set by a call # to the constructor. INITOPT = ['initopt'] _DEFAULT_OPTION_VALUE = ['default_option_value'] _useTkOptionDb = 0 # Symbolic constants for the indexes into an optionInfo list. _OPT_DEFAULT = 0 _OPT_VALUE = 1 _OPT_FUNCTION = 2 # Stacks _busyStack = [] # Stack which tracks nested calls to show/hidebusycursor (called # either directly or from activate()/deactivate()). Each element # is a dictionary containing: # 'newBusyWindows' : List of windows which had busy_hold called # on them during a call to showbusycursor(). # The corresponding call to hidebusycursor() # will call busy_release on these windows. # 'busyFocus' : The blt _Busy window which showbusycursor() # set the focus to. # 'previousFocus' : The focus as it was when showbusycursor() # was called. The corresponding call to # hidebusycursor() will restore this focus if # the focus has not been changed from busyFocus. _grabStack = [] # Stack of grabbed windows. It tracks calls to push/popgrab() # (called either directly or from activate()/deactivate()). The # window on the top of the stack is the window currently with the # grab. Each element is a dictionary containing: # 'grabWindow' : The window grabbed by pushgrab(). The # corresponding call to popgrab() will release # the grab on this window and restore the grab # on the next window in the stack (if there is one). # 'globalMode' : True if the grabWindow was grabbed with a # global grab, false if the grab was local # and 'nograb' if no grab was performed. # 'previousFocus' : The focus as it was when pushgrab() # was called. The corresponding call to # popgrab() will restore this focus. # 'deactivateFunction' : # The function to call (usually grabWindow.deactivate) if # popgrab() is called (usually from a deactivate() method) # on a window which is not at the top of the stack (that is, # does not have the grab or focus). For example, if a modal # dialog is deleted by the window manager or deactivated by # a timer. In this case, all dialogs above and including # this one are deactivated, starting at the top of the # stack. # Note that when dealing with focus windows, the name of the Tk # widget is used, since it may be the '_Busy' window, which has no # python instance associated with it. #============================================================================= # Functions used to forward methods from a class to a component. # Fill in a flattened method resolution dictionary for a class (attributes are # filtered out). Flattening honours the MI method resolution rules # (depth-first search of bases in order). The dictionary has method names # for keys and functions for values. def __methodDict(cls, d): # the strategy is to traverse the class in the _reverse_ of the normal # order, and overwrite any duplicates. baseList = list(cls.__bases__) baseList.reverse() # do bases in reverse order, so first base overrides last base for s in baseList: __methodDict(s, d) # do my methods last to override base classes for key, value in cls.__dict__.items(): # ignore class attributes if isinstance(value, Callable): d[key] = value def __methods(cls): # Return all method names for a class. # Return all method names for a class (attributes are filtered # out). Base classes are searched recursively. d = {} __methodDict(cls, d) return d.keys() # Function body to resolve a forwarding given the target method name and the # attribute name. The resulting lambda requires only self, but will forward # any other parameters. __stringBody = ( 'def %(method)s(this, *args, **kw): return ' + 'this.%(attribute)s.%(method)s(*args, **kw)') # Get a unique id __counter = 0 def __unique(): global __counter __counter = __counter + 1 return str(__counter) # Function body to resolve a forwarding given the target method name and the # index of the resolution function. The resulting lambda requires only self, # but will forward any other parameters. The target instance is identified # by invoking the resolution function. __funcBody = ( 'def %(method)s(this, *args, **kw): return ' + 'this.%(forwardFunc)s().%(method)s(*args, **kw)') def forwardmethods(fromClass, toClass, toPart, exclude=()): # Forward all methods from one class to another. # Forwarders will be created in fromClass to forward method # invocations to toClass. The methods to be forwarded are # identified by flattening the interface of toClass, and excluding # methods identified in the exclude list. Methods already defined # in fromClass, or special methods with one or more leading or # trailing underscores will not be forwarded. # For a given object of class fromClass, the corresponding toClass # object is identified using toPart. This can either be a String # denoting an attribute of fromClass objects, or a function taking # a fromClass object and returning a toClass object. # Example: # class MyClass: # ... # def __init__(self): # ... # self.__target = TargetClass() # ... # def findtarget(self): # return self.__target # forwardmethods(MyClass, TargetClass, '__target', ['dangerous1', 'dangerous2']) # # ...or... # forwardmethods(MyClass, TargetClass, MyClass.findtarget, # ['dangerous1', 'dangerous2']) # In both cases, all TargetClass methods will be forwarded from # MyClass except for dangerous1, dangerous2, special methods like # __str__, and pre-existing methods like findtarget. # Allow an attribute name (String) or a function to determine the instance if not _myisstr(toPart): # check that it is something like a function if isinstance(toPart, Callable): # If a method is passed, use the function within it if hasattr(toPart, 'im_func'): toPart = toPart.im_func # After this is set up, forwarders in this class will use # the forwarding function. The forwarding function name is # guaranteed to be unique, so that it can't be hidden by subclasses forwardName = '__fwdfunc__' + __unique() setattr(fromClass, forwardName, toPart) # It's not a valid type else: raise TypeError('toPart must be attribute name, function or method') # get the full set of candidate methods d = {} __methodDict(toClass, d) # discard special methods tmp = [ex for ex in d.keys() if ex[:1] == '_' or ex[-1:] == '_'] for i in tmp: del d[i] # discard dangerous methods supplied by the caller for ex in exclude: if ex in d: del d[ex] # discard methods already defined in fromClass for ex in __methods(fromClass): if ex in d: del d[ex] for method, func in d.items(): di = {'method': method, 'func': func} if _myisstr(toPart): execString = \ __stringBody % {'method': method, 'attribute': toPart} else: execString = \ __funcBody % {'forwardFunc': forwardName, 'method': method} exec(execString, di) # this creates a method setattr(fromClass, method, di[method]) #============================================================================= def setgeometryanddeiconify(window, geom): # To avoid flashes on X and to position the window correctly on NT # (caused by Tk bugs). if os.name == 'nt' or \ (os.name == 'posix' and sys.platform[:6] == 'cygwin'): # Require overrideredirect trick to stop window frame # appearing momentarily. redirect = window.overrideredirect() if not redirect: window.overrideredirect(1) window.deiconify() if geom is not None: window.geometry(geom) # Call update_idletasks to ensure NT moves the window to the # correct position it is raised. window.update_idletasks() window.tkraise() if not redirect: window.overrideredirect(0) else: if geom is not None: window.geometry(geom) # Problem!? Which way around should the following two calls # go? If deiconify() is called first then I get complaints # from people using the enlightenment or sawfish window # managers that when a dialog is activated it takes about 2 # seconds for the contents of the window to appear. But if # tkraise() is called first then I get complaints from people # using the twm window manager that when a dialog is activated # it appears in the top right corner of the screen and also # takes about 2 seconds to appear. #window.tkraise() # Call update_idletasks to ensure certain window managers (eg: # enlightenment and sawfish) do not cause Tk to delay for # about two seconds before displaying window. #window.update_idletasks() #window.deiconify() window.deiconify() if window.overrideredirect(): # The window is not under the control of the window manager # and so we need to raise it ourselves. window.tkraise() #============================================================================= class MegaArchetype(object): # Megawidget abstract root class. # This class provides methods which are inherited by classes # implementing useful bases (this class doesn't provide a # container widget inside which the megawidget can be built). def __init__(self, parent=None, hullClass=None): # Mapping from each megawidget option to a list of information # about the option # - default value # - current value # - function to call when the option is initialised in the # call to initialiseoptions() in the constructor or # modified via configure(). If this is INITOPT, the # option is an initialisation option (an option that can # be set by the call to the constructor but can not be # used with configure). # This mapping is not initialised here, but in the call to # defineoptions() which precedes construction of this base class. # # self._optionInfo = {} # Mapping from each component name to a tuple of information # about the component. # - component widget instance # - configure function of widget instance # - the class of the widget (Frame, EntryField, etc) # - cget function of widget instance # - the name of the component group of this component, if any self.__componentInfo = {} # Mapping from alias names to the names of components or # sub-components. self.__componentAliases = {} # Contains information about the keywords provided to the # constructor. It is a mapping from the keyword to a tuple # containing: # - value of keyword # - a boolean indicating if the keyword has been used. # A keyword is used if, during the construction of a megawidget, # - it is defined in a call to defineoptions() or addoptions(), or # - it references, by name, a component of the megawidget, or # - it references, by group, at least one component # At the end of megawidget construction, a call is made to # initialiseoptions() which reports an error if there are # unused options given to the constructor. # # After megawidget construction, the dictionary contains # keywords which refer to a dynamic component group, so that # these components can be created after megawidget # construction and still use the group options given to the # constructor. # # self._constructorKeywords = {} # List of dynamic component groups. If a group is included in # this list, then it not an error if a keyword argument for # the group is given to the constructor or to configure(), but # no components with this group have been created. # self._dynamicGroups = () if hullClass is None: self._hull = None else: if parent is None: parent = Tkinter._default_root # Create the hull. self._hull = self.createcomponent('hull', (), None, hullClass, (parent,)) _hullToMegaWidget[self._hull] = self if _useTkOptionDb: # Now that a widget has been created, query the Tk # option database to get the default values for the # options which have not been set in the call to the # constructor. This assumes that defineoptions() is # called before the __init__(). option_get = self.option_get _VALUE = _OPT_VALUE _DEFAULT = _OPT_DEFAULT for name, info in self._optionInfo.items(): value = info[_VALUE] if value is _DEFAULT_OPTION_VALUE: resourceClass = name[0].upper() + name[1:] value = option_get(name, resourceClass) if value != '': try: # Convert the string to int/float/tuple, etc value = eval(value, {'__builtins__': {}}) except: pass info[_VALUE] = value else: info[_VALUE] = info[_DEFAULT] def destroy(self): # Clean up optionInfo in case it contains circular references # in the function field, such as self._settitle in class # MegaToplevel. self._optionInfo = {} if self._hull is not None: del _hullToMegaWidget[self._hull] self._hull.destroy() #====================================================================== # Methods used (mainly) during the construction of the megawidget. def defineoptions(self, keywords, optionDefs, dynamicGroups=()): # Create options, providing the default value and the method # to call when the value is changed. If any option created by # base classes has the same name as one in , the # base class's value and function will be overriden. # This should be called before the constructor of the base # class, so that default values defined in the derived class # override those in the base class. if not hasattr(self, '_constructorKeywords'): # First time defineoptions has been called. tmp = {} for option, value in keywords.items(): tmp[option] = [value, 0] self._constructorKeywords = tmp self._optionInfo = {} self._initialiseoptions_counter = 0 self._initialiseoptions_counter = self._initialiseoptions_counter + 1 if not hasattr(self, '_dynamicGroups'): self._dynamicGroups = () self._dynamicGroups = self._dynamicGroups + tuple(dynamicGroups) self.addoptions(optionDefs) def addoptions(self, optionDefs): # Add additional options, providing the default value and the # method to call when the value is changed. See # "defineoptions" for more details # optimisations: optionInfo = self._optionInfo keywords = self._constructorKeywords FUNCTION = _OPT_FUNCTION for name, default, function in optionDefs: if '_' not in name: # The option will already exist if it has been defined # in a derived class. In this case, do not override the # default value of the option or the callback function # if it is not None. if name not in optionInfo: if name in keywords: value = keywords[name][0] optionInfo[name] = [default, value, function] del keywords[name] else: if _useTkOptionDb: optionInfo[name] = \ [default, _DEFAULT_OPTION_VALUE, function] else: optionInfo[name] = [default, default, function] elif optionInfo[name][FUNCTION] is None: optionInfo[name][FUNCTION] = function else: # This option is of the form "component_option". If this is # not already defined in self._constructorKeywords add it. # This allows a derived class to override the default value # of an option of a component of a base class. if name not in keywords: keywords[name] = [default, 0] def createcomponent(self, componentName, componentAliases, componentGroup, widgetClass, *widgetArgs, **kw): # Create a component (during construction or later). if componentName in self.__componentInfo: raise ValueError('Component "%s" already exists' % componentName) if '_' in componentName: raise ValueError( 'Component name "%s" must not contain "_"' % componentName) if hasattr(self, '_constructorKeywords'): keywords = self._constructorKeywords else: keywords = {} for alias, component in componentAliases: # Create aliases to the component and its sub-components. index = component.find('_') if index < 0: self.__componentAliases[alias] = (component, None) else: mainComponent = component[:index] subComponent = component[(index + 1):] self.__componentAliases[alias] = (mainComponent, subComponent) # Remove aliases from the constructor keyword arguments by # replacing any keyword arguments that begin with *alias* # with corresponding keys beginning with *component*. alias = alias + '_' aliasLen = len(alias) for option in keywords.keys(): if len(option) > aliasLen and option[:aliasLen] == alias: newkey = component + '_' + option[aliasLen:] keywords[newkey] = keywords[option] del keywords[option] componentPrefix = componentName + '_' nameLen = len(componentPrefix) tmp = [] for option in keywords.keys(): if len(option) > nameLen and option[:nameLen] == componentPrefix: # The keyword argument refers to this component, so add # this to the options to use when constructing the widget. kw[option[nameLen:]] = keywords[option][0] tmp.append(option) else: # Check if this keyword argument refers to the group # of this component. If so, add this to the options # to use when constructing the widget. Mark the # keyword argument as being used, but do not remove it # since it may be required when creating another # component. index = option.find('_') if index >= 0 and componentGroup == option[:index]: rest = option[(index + 1):] kw[rest] = keywords[option][0] keywords[option][1] = 1 for i in tmp: del keywords[i] if 'pyclass' in kw: widgetClass = kw['pyclass'] del kw['pyclass'] if widgetClass is None: return None if len(widgetArgs) == 1 and isinstance(widgetArgs[0], tuple): # Arguments to the constructor can be specified as either # multiple trailing arguments to createcomponent() or as a # single tuple argument. widgetArgs = widgetArgs[0] widget = widgetClass(*widgetArgs, **kw) componentClass = widget.__class__.__name__ self.__componentInfo[componentName] = (widget, widget.configure, componentClass, widget.cget, componentGroup) return widget def destroycomponent(self, name): # Remove a megawidget component. # This command is for use by megawidget designers to destroy a # megawidget component. self.__componentInfo[name][0].destroy() del self.__componentInfo[name] def createlabel(self, parent, childCols=1, childRows=1): labelpos = self['labelpos'] labelmargin = self['labelmargin'] if labelpos is None: return label = self.createcomponent('label', (), None, Tkinter.Label, (parent,)) if labelpos[0] in 'ns': # vertical layout if labelpos[0] == 'n': row = 0 margin = 1 else: row = childRows + 3 margin = row - 1 label.grid(column=2, row=row, columnspan=childCols, sticky=labelpos) parent.grid_rowconfigure(margin, minsize=labelmargin) else: # horizontal layout if labelpos[0] == 'w': col = 0 margin = 1 else: col = childCols + 3 margin = col - 1 label.grid(column=col, row=2, rowspan=childRows, sticky=labelpos) parent.grid_columnconfigure(margin, minsize=labelmargin) def initialiseoptions(self, dummy=None): self._initialiseoptions_counter = self._initialiseoptions_counter - 1 if self._initialiseoptions_counter == 0: unusedOptions = [] keywords = self._constructorKeywords for name in keywords.keys(): used = keywords[name][1] if not used: # This keyword argument has not been used. If it # does not refer to a dynamic group, mark it as # unused. index = name.find('_') if index < 0 or name[:index] not in self._dynamicGroups: unusedOptions.append(name) if len(unusedOptions) > 0: if len(unusedOptions) == 1: text = 'Unknown option "' else: text = 'Unknown options "' raise KeyError(text + ', '.join(unusedOptions) + '" for ' + self.__class__.__name__) # Call the configuration callback function for every option. FUNCTION = _OPT_FUNCTION for info in self._optionInfo.values(): func = info[FUNCTION] if func is not None and func is not INITOPT: func() #====================================================================== # Method used to configure the megawidget. def configure(self, option=None, **kw): # Query or configure the megawidget options. # # If not empty, *kw* is a dictionary giving new # values for some of the options of this megawidget or its # components. For options defined for this megawidget, set # the value of the option to the new value and call the # configuration callback function, if any. For options of the # form _