home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / configHelpSourceEdit.py < prev    next >
Text File  |  2003-12-30  |  6KB  |  158 lines

  1. "Dialog to specify or edit the parameters for a user configured help source."
  2.  
  3. import os
  4.  
  5. from Tkinter import *
  6. import tkMessageBox
  7. import tkFileDialog
  8.  
  9. class GetHelpSourceDialog(Toplevel):
  10.     def __init__(self, parent, title, menuItem='', filePath=''):
  11.         """Get menu entry and url/ local file location for Additional Help
  12.  
  13.         User selects a name for the Help resource and provides a web url
  14.         or a local file as its source.  The user can enter a url or browse
  15.         for the file.
  16.  
  17.         """
  18.         Toplevel.__init__(self, parent)
  19.         self.configure(borderwidth=5)
  20.         self.resizable(height=FALSE, width=FALSE)
  21.         self.title(title)
  22.         self.transient(parent)
  23.         self.grab_set()
  24.         self.protocol("WM_DELETE_WINDOW", self.Cancel)
  25.         self.parent = parent
  26.         self.result = None
  27.         self.CreateWidgets()
  28.         self.menu.set(menuItem)
  29.         self.path.set(filePath)
  30.         self.withdraw() #hide while setting geometry
  31.         #needs to be done here so that the winfo_reqwidth is valid
  32.         self.update_idletasks()
  33.         #centre dialog over parent:
  34.         self.geometry("+%d+%d" %
  35.                       ((parent.winfo_rootx() + ((parent.winfo_width()/2)
  36.                                                 -(self.winfo_reqwidth()/2)),
  37.                         parent.winfo_rooty() + ((parent.winfo_height()/2)
  38.                                                 -(self.winfo_reqheight()/2)))))
  39.         self.deiconify() #geometry set, unhide
  40.         self.bind('<Return>', self.Ok)
  41.         self.wait_window()
  42.  
  43.     def CreateWidgets(self):
  44.         self.menu = StringVar(self)
  45.         self.path = StringVar(self)
  46.         self.fontSize = StringVar(self)
  47.         self.frameMain = Frame(self, borderwidth=2, relief=GROOVE)
  48.         self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
  49.         labelMenu = Label(self.frameMain, anchor=W, justify=LEFT,
  50.                           text='Menu Item:')
  51.         self.entryMenu = Entry(self.frameMain, textvariable=self.menu,
  52.                                width=30)
  53.         self.entryMenu.focus_set()
  54.         labelPath = Label(self.frameMain, anchor=W, justify=LEFT,
  55.                           text='Help File Path: Enter URL or browse for file')
  56.         self.entryPath = Entry(self.frameMain, textvariable=self.path,
  57.                                width=40)
  58.         self.entryMenu.focus_set()
  59.         labelMenu.pack(anchor=W, padx=5, pady=3)
  60.         self.entryMenu.pack(anchor=W, padx=5, pady=3)
  61.         labelPath.pack(anchor=W, padx=5, pady=3)
  62.         self.entryPath.pack(anchor=W, padx=5, pady=3)
  63.         browseButton = Button(self.frameMain, text='Browse', width=8,
  64.                               command=self.browseFile)
  65.         browseButton.pack(pady=3)
  66.         frameButtons = Frame(self)
  67.         frameButtons.pack(side=BOTTOM, fill=X)
  68.         self.buttonOk = Button(frameButtons, text='OK',
  69.                                width=8, default=ACTIVE,  command=self.Ok)
  70.         self.buttonOk.grid(row=0, column=0, padx=5,pady=5)
  71.         self.buttonCancel = Button(frameButtons, text='Cancel',
  72.                                    width=8, command=self.Cancel)
  73.         self.buttonCancel.grid(row=0, column=1, padx=5, pady=5)
  74.  
  75.     def browseFile(self):
  76.         filetypes = [
  77.             ("HTML Files", "*.htm *.html", "TEXT"),
  78.             ("PDF Files", "*.pdf", "TEXT"),
  79.             ("Windows Help Files", "*.chm"),
  80.             ("Text Files", "*.txt", "TEXT"),
  81.             ("All Files", "*")]
  82.         path = self.path.get()
  83.         if path:
  84.             dir, base = os.path.split(path)
  85.         else:
  86.             base = None
  87.             if sys.platform.count('win') or sys.platform.count('nt'):
  88.                 dir = os.path.join(os.path.dirname(sys.executable), 'Doc')
  89.                 if not os.path.isdir(dir):
  90.                     dir = os.getcwd()
  91.             else:
  92.                 dir = os.getcwd()
  93.         opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes)
  94.         file = opendialog.show(initialdir=dir, initialfile=base)
  95.         if file:
  96.             self.path.set(file)
  97.  
  98.     def MenuOk(self):
  99.         "Simple validity check for a sensible menu item name"
  100.         menuOk = True
  101.         menu = self.menu.get()
  102.         menu.strip()
  103.         if not menu:
  104.             tkMessageBox.showerror(title='Menu Item Error',
  105.                                    message='No menu item specified',
  106.                                    parent=self)
  107.             self.entryMenu.focus_set()
  108.             menuOk = False
  109.         elif len(menu) > 30:
  110.             tkMessageBox.showerror(title='Menu Item Error',
  111.                                    message='Menu item too long:'
  112.                                            '\nLimit 30 characters.',
  113.                                    parent=self)
  114.             self.entryMenu.focus_set()
  115.             menuOk = False
  116.         return menuOk
  117.  
  118.     def PathOk(self):
  119.         "Simple validity check for menu file path"
  120.         pathOk = True
  121.         path = self.path.get()
  122.         path.strip()
  123.         if not path: #no path specified
  124.             tkMessageBox.showerror(title='File Path Error',
  125.                                    message='No help file path specified.',
  126.                                    parent=self)
  127.             self.entryPath.focus_set()
  128.             pathOk = False
  129.         elif path.startswith('www.') or path.startswith('http'):
  130.             pathOk = True
  131.         elif not os.path.exists(path):
  132.             tkMessageBox.showerror(title='File Path Error',
  133.                                    message='Help file path does not exist.',
  134.                                    parent=self)
  135.             self.entryPath.focus_set()
  136.             pathOk = False
  137.         return pathOk
  138.  
  139.     def Ok(self, event=None):
  140.         if self.MenuOk() and self.PathOk():
  141.             self.result = (self.menu.get().strip(),
  142.                            self.path.get().strip())
  143.             self.destroy()
  144.  
  145.     def Cancel(self, event=None):
  146.         self.result = None
  147.         self.destroy()
  148.  
  149. if __name__ == '__main__':
  150.     #test the dialog
  151.     root = Tk()
  152.     def run():
  153.         keySeq = ''
  154.         dlg = GetHelpSourceDialog(root, 'Get Help Source')
  155.         print dlg.result
  156.     Button(root,text='Dialog', command=run).pack()
  157.     root.mainloop()
  158.