WmDefault.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: WmDefault.py,v 1.2 2001/12/09 05:03:09 idiscovery Exp $
  4. #
  5. """One of the bad things about Tk/Tkinter is that it does not pick up
  6. the current color and font scheme from the prevailing CDE/KDE/GNOME/Windows
  7. window manager scheme.
  8. One of the good things about Tk/Tkinter is that it is not tied to one
  9. particular widget set so it could pick up the current color and font scheme
  10. from the prevailing CDE/KDE/GNOME/Windows window manager scheme.
  11. The WmDefault package is for making Tk/Tkinter applications use the
  12. prevailing CDE/KDE/GNOME/Windows scheme. It tries to find the files
  13. and/or settings that the current window manager is using, and then
  14. sets the Tk options database accordingly.
  15. Download the latest version of wm_default from http://tix.sourceforge.net
  16. either as a part of the standard Tix distribution, or as a part of the
  17. Tix Applications: http://tix.sourceforge.net/Tide. wm_default does not
  18. require Tix, but is Tix enabled.
  19. """
  20. import os, sys, traceback, string
  21. import tkMessageBox
  22. def setup(root, wm=''):
  23. """1) find the files and/or settings (::wm_default::setup).
  24. Takes one optional argument: wm, the name of the window manager
  25. as a string, if known. One of: windows gnome kde1 kde2 cde kde.
  26. """
  27. try:
  28. try:
  29. # Make sure Tcl/Tk knows wm_default is installed
  30. root.tk.eval("package require wm_default")
  31. except:
  32. # Try again with this directory on the Tcl/Tk path
  33. dir = os.path.dirname (self.__file__)
  34. root.tk.eval('global auto_path; lappend auto_path {%s}' % dir)
  35. root.tk.eval("package require wm_default")
  36. except:
  37. t, v, tb = sys.exc_info()
  38. text = "Error loading WmDefault\n"
  39. for line in traceback.format_exception(t,v,tb): text = text + line + '\n'
  40. try:
  41. tkMessageBox.showerror ('WmDefault Error', text)
  42. except:
  43. sys.stderr.write( text )
  44. return root.tk.call('::wm_default::setup', wm)
  45. def addoptions(root, cnf=None, **kw):
  46. """2) Setting the Tk options database (::wm_default::addoptions).
  47. You can override the settings in 1) by adding your values to the
  48. call to addoptions().
  49. """
  50. if cnf is None:
  51. return root.tk.splitlist(root.tk.call('::wm_default::addoptions'))
  52. return root.tk.splitlist(
  53. apply(root.tk.call,
  54. ('::wm_default::addoptions',) + root._options(cnf,kw)))
  55. def getoptions(root):
  56. """Returns the current settings, as a dictionary.
  57. """
  58. words = root.tk.splitlist(root.tk.call('::wm_default::getoptions'))
  59. dict = {}
  60. for i in range(0, len(words), 2):
  61. key = words[i]
  62. value = words[i+1]
  63. dict[key] = value
  64. return dict
  65. def parray(root):
  66. """Returns a string of the current settings, one value-pair per line.
  67. """
  68. return root.tk.call('::wm_default::parray')
  69. if __name__ == "__main__":
  70. dir = ""
  71. if len(sys.argv) > 0:
  72. # Assume the name of the file containing the tixinspect Tcl source
  73. # is the same as argument on the command line with .tcl
  74. dir = os.path.dirname(sys.argv[0])
  75. if not dir or not os.path.isdir(dir) or not os.path.isabs(dir):
  76. # Or, assume it's in the same directory as this one:
  77. dir = os.getcwd()
  78. import Tkinter
  79. root = Tkinter.Tk()
  80. setup(root)
  81. addoptions(root, {'foreground': 'red'})
  82. retval = getoptions(root)
  83. print retval