Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # -*- python -*-
- import sys
- import os
- from os.path import join,getsize,exists
- import tkMessageBox
- import tkFileDialog
- py2 = py30 = py31 = False
- version = sys.hexversion
- if version >= 0x020600F0 and version < 0x03000000 :
- py2 = True # Python 2.6 or 2.7
- from Tkinter import *
- import ttk
- import tkFont
- elif version >= 0x03000000 and version < 0x03010000 :
- py30 = True
- from tkinter import *
- import ttk
- elif version >= 0x03010000:
- py31 = True
- from tkinter import *
- import tkinter.ttk as ttk
- else:
- print ("""
- You do not have a version of python supporting ttk widgets..
- You need a version >= 2.6 to execute PAGE modules.
- """)
- sys.exit()
- def vp_start_gui():
- '''Starting point when module is the main routine.'''
- global val, w, root
- root = Tk()
- root.title('Searcher')
- root.geometry('600x525+413+86')
- set_Tk_var()
- w = Searcher (root)
- init()
- root.mainloop()
- w = None
- def create_Searcher (root):
- '''Starting point when module is imported by another program.'''
- global w, w_win
- if w: # So we have only one instance of window.
- return
- w = Toplevel (root)
- w.title('Searcher')
- w.geometry('600x525+413+86')
- set_Tk_var()
- w_win = Searcher (w)
- init()
- return w_win
- def destroy_Searcher ():
- global w
- w.destroy()
- w = None
- def set_Tk_var():
- # These are Tk variables used passed to Tkinter and must be
- # defined before the widgets using them are created.
- global FilePath
- FilePath = StringVar()
- global VchkAVI
- VchkAVI = StringVar()
- global VchkMKV
- VchkMKV = StringVar()
- global VchkMP3
- VchkMP3 = StringVar()
- global VchkMV4
- VchkMV4 = StringVar()
- global VchkOGG
- VchkOGG = StringVar()
- global exts, FileList
- exts = []
- FilePath=StringVar()
- FileList=[]
- #-------------
- global busyCursor,preBusyCursors,busyWidgets
- busyCursor = 'watch'
- preBusyCursors = None
- busyWidgets = (root, )
- #-------------
- def btnExitClick(p1) :
- sys.exit()
- def btnGoClick(p1) :
- #pass
- busyStart()
- BuildExts()
- fp = FilePath.get()
- e1 = tuple(exts)
- Walkit(fp,e1)
- LoadDataGrid()
- busyEnd()
- def btnSearchPathClick(p1) :
- #pass
- path = tkFileDialog.askdirectory() #**self.file_opt)
- FilePath.set(path)
- def init():
- #pass
- # Fires AFTER Widgets and Window are created...
- global treeview
- BlankChecks()
- treeview = w.Scrolledtreeview1
- SetupTreeview()
- def BlankChecks():
- VchkAVI.set('0')
- VchkMKV.set('0')
- VchkMP3.set('0')
- VchkMV4.set('0')
- VchkOGG.set('0')
- def BuildExts():
- if VchkAVI.get() == '1':
- exts.append(".avi")
- if VchkMKV.get() == '1':
- exts.append(".mkv")
- if VchkMP3.get() == '1':
- exts.append(".mp3")
- if VchkMV4.get() == '1':
- exts.append(".mv4")
- if VchkOGG.get() == '1':
- exts.append(".ogg")
- def btnSearchPathClick(p1) :
- path = tkFileDialog.askdirectory() #**self.file_opt)
- FilePath.set(path)
- def Walkit(musicpath,extensions):
- rcntr = 0
- fl = []
- for root, dirs, files in os.walk(musicpath):
- rcntr += 1 # This is the number of folders we have walked
- for file in [f for f in files if f.endswith(extensions)]:
- fl.append(file)
- fl.append(root)
- FileList.append(fl)
- fl=[]
- def SetupTreeview():
- global ColHeads
- ColHeads = ['Filename','Path']
- treeview.configure(columns=ColHeads,
- show="headings")
- for col in ColHeads:
- treeview.heading(col, text = col.title(),
- command = lambda c = col: sortby(treeview, c, 0))
- ## adjust the column's width to the header string
- treeview.column(col, width = tkFont.Font().measure(col.title()))
- def ClearDataGrid():
- for c in treeview.get_children(''):
- treeview.delete(c)
- def LoadDataGrid():
- global ColHeads
- for c in FileList:
- treeview.insert('','end',values=c)
- # adjust column's width if necessary to fit each value
- for ix, val in enumerate(c):
- col_w = tkFont.Font().measure(val)
- if treeview.column(ColHeads[ix],width=None)<col_w:
- treeview.column(ColHeads[ix], width=col_w)
- def busyStart(newcursor=None):
- global preBusyCursors
- if not newcursor:
- newcursor = busyCursor
- newPreBusyCursors = {}
- for component in busyWidgets:
- newPreBusyCursors[component] = component['cursor']
- component.configure(cursor=newcursor)
- component.update_idletasks()
- preBusyCursors = (newPreBusyCursors, preBusyCursors)
- def busyEnd():
- global preBusyCursors
- if not preBusyCursors:
- return
- oldPreBusyCursors = preBusyCursors[0]
- preBusyCursors = preBusyCursors[1]
- for component in busyWidgets:
- try:
- component.configure(cursor=oldPreBusyCursors[component])
- except KeyError:
- pass
- component.update_idletasks()
- class Searcher:
- def __init__(self, master=None):
- # Set background of toplevel window to match
- # current style
- style = ttk.Style()
- theme = style.theme_use()
- default = style.lookup(theme, 'background')
- master.configure(background=default)
- self.Frame1 = Frame (master)
- self.Frame1.place(relx=0.0,rely=0.0,relheight=0.1,relwidth=0.99)
- self.Frame1.configure(relief=GROOVE)
- self.Frame1.configure(borderwidth="2")
- self.Frame1.configure(relief="groove")
- self.btnExit = Button (self.Frame1)
- self.btnExit.place(relx=0.86,rely=0.36,height=27,width=49)
- self.btnExit.configure(text='''Exit''')
- self.btnExit.bind('<Button-1>',btnExitClick)
- self.Frame2 = Frame (master)
- self.Frame2.place(relx=0.0,rely=0.11,relheight=0.35,relwidth=0.54)
- self.Frame2.configure(relief=GROOVE)
- self.Frame2.configure(borderwidth="2")
- self.Frame2.configure(relief="groove")
- self.Label1 = Label (self.Frame2)
- self.Label1.place(relx=0.03,rely=0.05,height=19,width=35)
- self.Label1.configure(text='''Path:''')
- self.txtPath = Entry (self.Frame2)
- self.txtPath.place(relx=0.03,rely=0.16,relheight=0.11,relwidth=0.7)
- self.txtPath.configure(textvariable=FilePath)
- self.btnSearchPath = Button (self.Frame2)
- self.btnSearchPath.place(relx=0.74,rely=0.16,height=27,width=38)
- self.btnSearchPath.configure(text='''...''')
- self.btnSearchPath.bind('<Button-1>',btnSearchPathClick)
- self.chkAVI = Checkbutton (self.Frame2)
- self.chkAVI.place(relx=0.15,rely=0.43,relheight=0.11,relwidth=0.14)
- self.chkAVI.configure(text='''.avi''')
- self.chkAVI.configure(variable=VchkAVI)
- self.chkMKV = Checkbutton (self.Frame2)
- self.chkMKV.place(relx=0.15,rely=0.54,relheight=0.11,relwidth=0.17)
- self.chkMKV.configure(text='''.mkv''')
- self.chkMKV.configure(variable=VchkMKV)
- self.chkMV4 = Checkbutton (self.Frame2)
- self.chkMV4.place(relx=0.15,rely=0.65,relheight=0.11,relwidth=0.18)
- self.chkMV4.configure(text='''.mv4''')
- self.chkMV4.configure(variable=VchkMV4)
- self.chkMP3 = Checkbutton (self.Frame2)
- self.chkMP3.place(relx=0.58,rely=0.54,relheight=0.11,relwidth=0.18)
- self.chkMP3.configure(text='''.mp3''')
- self.chkMP3.configure(variable=VchkMP3)
- self.chkOGG = Checkbutton (self.Frame2)
- self.chkOGG.place(relx=0.58,rely=0.65,relheight=0.11,relwidth=0.16)
- self.chkOGG.configure(text='''.ogg''')
- self.chkOGG.configure(variable=VchkOGG)
- self.btnGO = Button (self.Frame2)
- self.btnGO.place(relx=0.4,rely=0.81,height=27,width=49)
- self.btnGO.configure(text='''GO!''')
- self.btnGO.bind('<Button-1>',btnGoClick)
- self.Frame3 = Frame (master)
- self.Frame3.place(relx=0.0,rely=0.48,relheight=0.5,relwidth=0.94)
- self.Frame3.configure(relief=GROOVE)
- self.Frame3.configure(borderwidth="2")
- self.Frame3.configure(relief="groove")
- self.Scrolledtreeview1 = ScrolledTreeView (self.Frame3)
- self.Scrolledtreeview1.place(relx=0.02,rely=0.04,relheight=0.96
- ,relwidth=0.97)
- self.Scrolledtreeview1.configure(columns="Col1")
- self.Scrolledtreeview1.configure(takefocus="ttk::takefocus")
- self.Scrolledtreeview1.heading("#0",text="Tree")
- self.Scrolledtreeview1.heading("#0",anchor="center")
- self.Scrolledtreeview1.column("#0",width="266")
- self.Scrolledtreeview1.column("#0",minwidth="20")
- self.Scrolledtreeview1.column("#0",stretch="1")
- self.Scrolledtreeview1.column("#0",anchor="w")
- self.Scrolledtreeview1.heading("Col1",text="Col1")
- self.Scrolledtreeview1.heading("Col1",anchor="center")
- self.Scrolledtreeview1.column("Col1",width="267")
- self.Scrolledtreeview1.column("Col1",minwidth="20")
- self.Scrolledtreeview1.column("Col1",stretch="1")
- self.Scrolledtreeview1.column("Col1",anchor="w")
- # The following code is added to facilitate the Scrolled widgets you specified.
- class AutoScroll(object):
- '''Configure the scrollbars for a widget.'''
- def __init__(self, master):
- vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
- hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)
- self.configure(yscrollcommand=self._autoscroll(vsb),
- xscrollcommand=self._autoscroll(hsb))
- self.grid(column=0, row=0, sticky='nsew')
- vsb.grid(column=1, row=0, sticky='ns')
- hsb.grid(column=0, row=1, sticky='ew')
- master.grid_columnconfigure(0, weight=1)
- master.grid_rowconfigure(0, weight=1)
- # Copy geometry methods of master (took from ScrolledText.py)
- methods = Pack.__dict__.keys() + Grid.__dict__.keys() \
- + Place.__dict__.keys()
- for meth in methods:
- if meth[0] != '_' and meth not in ('config', 'configure'):
- setattr(self, meth, getattr(master, meth))
- @staticmethod
- def _autoscroll(sbar):
- '''Hide and show scrollbar as needed.'''
- def wrapped(first, last):
- first, last = float(first), float(last)
- if first <= 0 and last >= 1:
- sbar.grid_remove()
- else:
- sbar.grid()
- sbar.set(first, last)
- return wrapped
- def __str__(self):
- return str(self.master)
- def _create_container(func):
- '''Creates a ttk Frame with a given master, and use this new frame to
- place the scrollbars and the widget.'''
- def wrapped(cls, master, **kw):
- container = ttk.Frame(master)
- return func(cls, container, **kw)
- return wrapped
- class ScrolledTreeView(AutoScroll, ttk.Treeview):
- '''A standard ttk Treeview widget with scrollbars that will
- automatically show/hide as needed.'''
- @_create_container
- def __init__(self, master, **kw):
- ttk.Treeview.__init__(self, master, **kw)
- AutoScroll.__init__(self, master)
- if __name__ == '__main__':
- vp_start_gui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement