File: pyedit-products/unzipped/PP4E/Gui/ShellGui/formrows.py
"""" ================================================================================ Create a label+entry row frame, with optional file open browse button. This is a separate module because it can save code in other programs too. Caller (or callbacks here): retain returned linked var while row is in use; Augmented dec-2016, post PP4E: 1) The browse button, if enabled, can now pick either file or folder, not just file (folders were introduced for Grep dialog in PyEdit) 2) Now sets either title or message in dialog (on Mac OS X, the latter is required and the first is ignored; On Windows/Linux, the latter is an error) 3) Stretch initial entry width to be 2 * label width, instead of using platform default (else often too narrow without user resizing) ================================================================================ """ import sys from tkinter import * # widgets and presets from tkinter.filedialog import askopenfilename # file selector dialog from tkinter.filedialog import askdirectory # folder selector dialog def makeFormRow(parent, label, width=15, browse=True, extend=False, folder=False, app=''): """ build one row in parent: label+entry, with an optional file/folder browse button configured by last 4 arguments; """ var = StringVar() row = Frame(parent) lab = Label(row, text=label + '?', relief=RIDGE, width=width) ent = Entry(row, relief=SUNKEN, textvariable=var, width=width*2) row.pack(fill=X) # uses packed row frames lab.pack(side=LEFT) # and fixed-width labels ent.pack(side=LEFT, expand=YES, fill=X) # or use grid(row, col) if browse: btn = Button(row, text='Browse...') btn.pack(side=RIGHT) # don't raise root above subject parent, do use slidedowns on Mac askargs = dict(parent=parent) # portability assist: Mac ignores title, Windows fails on message prompt = label if not app else (app + ': ' + label) if sys.platform.startswith('darwin'): askargs.update(dict(message=prompt)) # Mac OS X else: askargs.update(dict(title=prompt)) # Windows/Linux asker = askopenfilename if not folder else askdirectory if not extend: btn.config(command=lambda: [var.set(asker(**askargs) or var.get()), # 2-action combo parent.focus_force()] ) # refocus for Mac else: btn.config(command=lambda: [var.set(var.get() + ' ' + asker(**askargs)), parent.focus_force()] ) return var