Advertisement
gregwa

searcher.py

Mar 3rd, 2012
871
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python
  2. # -*- python -*-
  3.  
  4. import sys
  5. import os
  6. from os.path import join,getsize,exists
  7. import tkMessageBox
  8. import tkFileDialog
  9.  
  10. py2 = py30 = py31 = False
  11. version = sys.hexversion
  12. if version >= 0x020600F0 and version < 0x03000000 :
  13.     py2 = True    # Python 2.6 or 2.7
  14.     from Tkinter import *
  15.     import ttk
  16.     import tkFont
  17.        
  18. elif version >= 0x03000000 and version < 0x03010000 :
  19.     py30 = True
  20.     from tkinter import *
  21.     import ttk
  22. elif version >= 0x03010000:
  23.     py31 = True
  24.     from tkinter import *
  25.     import tkinter.ttk as ttk
  26. else:
  27.     print ("""
  28.    You do not have a version of python supporting ttk widgets..
  29.    You need a version >= 2.6 to execute PAGE modules.
  30.    """)
  31.     sys.exit()
  32.  
  33.  
  34.  
  35. def vp_start_gui():
  36.     '''Starting point when module is the main routine.'''
  37.     global val, w, root
  38.  
  39.     root = Tk()
  40.     root.title('Searcher')
  41.     root.geometry('600x525+413+86')
  42.  
  43.     set_Tk_var()
  44.     w = Searcher (root)
  45.     init()
  46.     root.mainloop()
  47.  
  48. w = None
  49. def create_Searcher (root):
  50.     '''Starting point when module is imported by another program.'''
  51.     global w, w_win
  52.     if w: # So we have only one instance of window.
  53.         return
  54.     w = Toplevel (root)
  55.     w.title('Searcher')
  56.     w.geometry('600x525+413+86')
  57.     set_Tk_var()
  58.     w_win = Searcher (w)
  59.     init()
  60.     return w_win
  61.  
  62. def destroy_Searcher ():
  63.     global w
  64.     w.destroy()
  65.     w = None
  66.  
  67.  
  68. def set_Tk_var():
  69.     # These are Tk variables used passed to Tkinter and must be
  70.     # defined before the widgets using them are created.
  71.     global FilePath
  72.     FilePath = StringVar()
  73.  
  74.     global VchkAVI
  75.     VchkAVI = StringVar()
  76.  
  77.     global VchkMKV
  78.     VchkMKV = StringVar()
  79.  
  80.     global VchkMP3
  81.     VchkMP3 = StringVar()
  82.  
  83.     global VchkMV4
  84.     VchkMV4 = StringVar()
  85.  
  86.     global VchkOGG
  87.     VchkOGG = StringVar()
  88.     global exts, FileList
  89.     exts = []
  90.     FilePath=StringVar()
  91.     FileList=[]
  92.     #-------------
  93.     global busyCursor,preBusyCursors,busyWidgets
  94.     busyCursor = 'watch'
  95.     preBusyCursors = None
  96.     busyWidgets = (root, )
  97.     #-------------    
  98.  
  99. def btnExitClick(p1) :
  100.     sys.exit()
  101.  
  102.  
  103. def btnGoClick(p1) :
  104.     #pass
  105.     busyStart()    
  106.     BuildExts()
  107.     fp = FilePath.get()
  108.     e1 = tuple(exts)
  109.     Walkit(fp,e1)
  110.     LoadDataGrid()    
  111.     busyEnd()
  112.  
  113. def btnSearchPathClick(p1) :
  114.     #pass
  115.     path = tkFileDialog.askdirectory() #**self.file_opt)
  116.     FilePath.set(path)  
  117.    
  118. def init():
  119.     #pass
  120.     # Fires AFTER Widgets and Window are created...
  121.     global treeview
  122.  
  123.     BlankChecks()    
  124.     treeview = w.Scrolledtreeview1
  125.     SetupTreeview()
  126.    
  127. def BlankChecks():
  128.     VchkAVI.set('0')
  129.     VchkMKV.set('0')
  130.     VchkMP3.set('0')
  131.     VchkMV4.set('0')
  132.     VchkOGG.set('0')
  133.    
  134. def BuildExts():
  135.     if VchkAVI.get() == '1':
  136.         exts.append(".avi")
  137.     if VchkMKV.get() == '1':
  138.         exts.append(".mkv")
  139.     if VchkMP3.get() == '1':
  140.         exts.append(".mp3")
  141.     if VchkMV4.get() == '1':
  142.         exts.append(".mv4")
  143.     if VchkOGG.get() == '1':
  144.         exts.append(".ogg")
  145.        
  146. def btnSearchPathClick(p1) :
  147.     path = tkFileDialog.askdirectory() #**self.file_opt)
  148.     FilePath.set(path)
  149.    
  150. def Walkit(musicpath,extensions):
  151.     rcntr = 0
  152.     fl = []
  153.     for root, dirs, files in os.walk(musicpath):
  154.         rcntr += 1  # This is the number of folders we have walked
  155.         for file in [f for f in files if f.endswith(extensions)]:
  156.             fl.append(file)
  157.             fl.append(root)
  158.             FileList.append(fl)
  159.             fl=[]
  160.            
  161. def SetupTreeview():
  162.     global ColHeads
  163.     ColHeads = ['Filename','Path']
  164.     treeview.configure(columns=ColHeads,
  165.         show="headings")
  166.     for col in ColHeads:
  167.         treeview.heading(col, text = col.title(),
  168.                      command = lambda c = col: sortby(treeview, c, 0))
  169.         ## adjust the column's width to the header string
  170.         treeview.column(col, width = tkFont.Font().measure(col.title()))      
  171.    
  172. def ClearDataGrid():
  173.     for c in treeview.get_children(''):
  174.         treeview.delete(c)
  175.    
  176. def LoadDataGrid():
  177.     global ColHeads
  178.                  
  179.     for c in FileList:
  180.         treeview.insert('','end',values=c)
  181.         # adjust column's width if necessary to fit each value
  182.         for ix, val in enumerate(c):
  183.             col_w = tkFont.Font().measure(val)
  184.             if treeview.column(ColHeads[ix],width=None)<col_w:
  185.                 treeview.column(ColHeads[ix], width=col_w)      
  186.  
  187. def busyStart(newcursor=None):
  188.     global preBusyCursors
  189.    
  190.     if not newcursor:
  191.         newcursor = busyCursor
  192.     newPreBusyCursors = {}
  193.     for component in busyWidgets:
  194.         newPreBusyCursors[component] = component['cursor']
  195.         component.configure(cursor=newcursor)
  196.         component.update_idletasks()
  197.     preBusyCursors = (newPreBusyCursors, preBusyCursors)
  198.  
  199.    
  200. def busyEnd():
  201.     global preBusyCursors
  202.     if not preBusyCursors:
  203.         return
  204.     oldPreBusyCursors = preBusyCursors[0]
  205.     preBusyCursors = preBusyCursors[1]
  206.     for component in busyWidgets:
  207.         try:
  208.             component.configure(cursor=oldPreBusyCursors[component])
  209.         except KeyError:
  210.             pass
  211.         component.update_idletasks()
  212.            
  213. class Searcher:
  214.     def __init__(self, master=None):
  215.         # Set background of toplevel window to match
  216.         # current style
  217.         style = ttk.Style()
  218.         theme = style.theme_use()
  219.         default = style.lookup(theme, 'background')
  220.         master.configure(background=default)
  221.  
  222.         self.Frame1 = Frame (master)
  223.         self.Frame1.place(relx=0.0,rely=0.0,relheight=0.1,relwidth=0.99)
  224.         self.Frame1.configure(relief=GROOVE)
  225.         self.Frame1.configure(borderwidth="2")
  226.         self.Frame1.configure(relief="groove")
  227.  
  228.         self.btnExit = Button (self.Frame1)
  229.         self.btnExit.place(relx=0.86,rely=0.36,height=27,width=49)
  230.         self.btnExit.configure(text='''Exit''')
  231.         self.btnExit.bind('<Button-1>',btnExitClick)
  232.  
  233.         self.Frame2 = Frame (master)
  234.         self.Frame2.place(relx=0.0,rely=0.11,relheight=0.35,relwidth=0.54)
  235.         self.Frame2.configure(relief=GROOVE)
  236.         self.Frame2.configure(borderwidth="2")
  237.         self.Frame2.configure(relief="groove")
  238.  
  239.         self.Label1 = Label (self.Frame2)
  240.         self.Label1.place(relx=0.03,rely=0.05,height=19,width=35)
  241.         self.Label1.configure(text='''Path:''')
  242.  
  243.         self.txtPath = Entry (self.Frame2)
  244.         self.txtPath.place(relx=0.03,rely=0.16,relheight=0.11,relwidth=0.7)
  245.         self.txtPath.configure(textvariable=FilePath)
  246.  
  247.         self.btnSearchPath = Button (self.Frame2)
  248.         self.btnSearchPath.place(relx=0.74,rely=0.16,height=27,width=38)
  249.         self.btnSearchPath.configure(text='''...''')
  250.         self.btnSearchPath.bind('<Button-1>',btnSearchPathClick)
  251.  
  252.         self.chkAVI = Checkbutton (self.Frame2)
  253.         self.chkAVI.place(relx=0.15,rely=0.43,relheight=0.11,relwidth=0.14)
  254.         self.chkAVI.configure(text='''.avi''')
  255.         self.chkAVI.configure(variable=VchkAVI)
  256.  
  257.         self.chkMKV = Checkbutton (self.Frame2)
  258.         self.chkMKV.place(relx=0.15,rely=0.54,relheight=0.11,relwidth=0.17)
  259.         self.chkMKV.configure(text='''.mkv''')
  260.         self.chkMKV.configure(variable=VchkMKV)
  261.  
  262.         self.chkMV4 = Checkbutton (self.Frame2)
  263.         self.chkMV4.place(relx=0.15,rely=0.65,relheight=0.11,relwidth=0.18)
  264.         self.chkMV4.configure(text='''.mv4''')
  265.         self.chkMV4.configure(variable=VchkMV4)
  266.  
  267.         self.chkMP3 = Checkbutton (self.Frame2)
  268.         self.chkMP3.place(relx=0.58,rely=0.54,relheight=0.11,relwidth=0.18)
  269.         self.chkMP3.configure(text='''.mp3''')
  270.         self.chkMP3.configure(variable=VchkMP3)
  271.  
  272.         self.chkOGG = Checkbutton (self.Frame2)
  273.         self.chkOGG.place(relx=0.58,rely=0.65,relheight=0.11,relwidth=0.16)
  274.         self.chkOGG.configure(text='''.ogg''')
  275.         self.chkOGG.configure(variable=VchkOGG)
  276.  
  277.         self.btnGO = Button (self.Frame2)
  278.         self.btnGO.place(relx=0.4,rely=0.81,height=27,width=49)
  279.         self.btnGO.configure(text='''GO!''')
  280.         self.btnGO.bind('<Button-1>',btnGoClick)
  281.  
  282.         self.Frame3 = Frame (master)
  283.         self.Frame3.place(relx=0.0,rely=0.48,relheight=0.5,relwidth=0.94)
  284.         self.Frame3.configure(relief=GROOVE)
  285.         self.Frame3.configure(borderwidth="2")
  286.         self.Frame3.configure(relief="groove")
  287.  
  288.         self.Scrolledtreeview1 = ScrolledTreeView (self.Frame3)
  289.         self.Scrolledtreeview1.place(relx=0.02,rely=0.04,relheight=0.96
  290.                 ,relwidth=0.97)
  291.         self.Scrolledtreeview1.configure(columns="Col1")
  292.         self.Scrolledtreeview1.configure(takefocus="ttk::takefocus")
  293.         self.Scrolledtreeview1.heading("#0",text="Tree")
  294.         self.Scrolledtreeview1.heading("#0",anchor="center")
  295.         self.Scrolledtreeview1.column("#0",width="266")
  296.         self.Scrolledtreeview1.column("#0",minwidth="20")
  297.         self.Scrolledtreeview1.column("#0",stretch="1")
  298.         self.Scrolledtreeview1.column("#0",anchor="w")
  299.         self.Scrolledtreeview1.heading("Col1",text="Col1")
  300.         self.Scrolledtreeview1.heading("Col1",anchor="center")
  301.         self.Scrolledtreeview1.column("Col1",width="267")
  302.         self.Scrolledtreeview1.column("Col1",minwidth="20")
  303.         self.Scrolledtreeview1.column("Col1",stretch="1")
  304.         self.Scrolledtreeview1.column("Col1",anchor="w")
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. # The following code is added to facilitate the Scrolled widgets you specified.
  312. class AutoScroll(object):
  313.     '''Configure the scrollbars for a widget.'''
  314.  
  315.     def __init__(self, master):
  316.         vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
  317.         hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)
  318.  
  319.         self.configure(yscrollcommand=self._autoscroll(vsb),
  320.             xscrollcommand=self._autoscroll(hsb))
  321.         self.grid(column=0, row=0, sticky='nsew')
  322.         vsb.grid(column=1, row=0, sticky='ns')
  323.         hsb.grid(column=0, row=1, sticky='ew')
  324.  
  325.         master.grid_columnconfigure(0, weight=1)
  326.         master.grid_rowconfigure(0, weight=1)
  327.  
  328.         # Copy geometry methods of master  (took from ScrolledText.py)
  329.         methods = Pack.__dict__.keys() + Grid.__dict__.keys() \
  330.                   + Place.__dict__.keys()
  331.  
  332.         for meth in methods:
  333.             if meth[0] != '_' and meth not in ('config', 'configure'):
  334.                 setattr(self, meth, getattr(master, meth))
  335.  
  336.     @staticmethod
  337.     def _autoscroll(sbar):
  338.         '''Hide and show scrollbar as needed.'''
  339.         def wrapped(first, last):
  340.             first, last = float(first), float(last)
  341.             if first <= 0 and last >= 1:
  342.                 sbar.grid_remove()
  343.             else:
  344.                 sbar.grid()
  345.             sbar.set(first, last)
  346.         return wrapped
  347.  
  348.     def __str__(self):
  349.         return str(self.master)
  350.  
  351. def _create_container(func):
  352.     '''Creates a ttk Frame with a given master, and use this new frame to
  353.    place the scrollbars and the widget.'''
  354.     def wrapped(cls, master, **kw):
  355.         container = ttk.Frame(master)
  356.         return func(cls, container, **kw)
  357.     return wrapped
  358.  
  359. class ScrolledTreeView(AutoScroll, ttk.Treeview):
  360.     '''A standard ttk Treeview widget with scrollbars that will
  361.    automatically show/hide as needed.'''
  362.     @_create_container
  363.     def __init__(self, master, **kw):
  364.         ttk.Treeview.__init__(self, master, **kw)
  365.         AutoScroll.__init__(self, master)
  366.  
  367.  
  368. if __name__ == '__main__':
  369.     vp_start_gui()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement