1

I would like to modify the tkFileDialog to include a Checkbutton for "open this file after saving."

The boolean value of this Checkbutton would determine whether os.startfile() gets called on the file name returned from the dialog, or not.

Example: how could I set the variable openMeNow in:

import tkinter as tk
from os import path, startfile
import pandas as pd

def getOutputFileName(initDir,title="Save Output as",initFile="the_output",defaultExt=''):
    root = tk.Tk()
    root.lift()
    root.attributes("-topmost", True)
    root.withdraw()   
    fOut = tk.filedialog.asksaveasfilename(title=title,
                                        initialdir=initDir,
                                        initialfile=initFile,
                                        defaultextension=defaultExt)
    return fOut

outExcel = path.normpath( getOutputFileName(
                            title="Save Workbook as",
                            initDir='~/Documents',
                            initFile='the_workbook.xlsx',
                            defaultExt='.xlsx'
           ) )  

# do some stuff that returns some results
results = pd.DataFrame({'a':[1,2],'b':[10,100],'c':[0,50]})

xlWriter = pd.ExcelWriter(outExcel, engine='xlsxwriter')
results.to_excel(xlWriter, sheet_name = 'The Results', index=False)           
xlWriter.save()

if openMeNow: startfile(outExcel)

The question is sort of similar to this one, with the difference being that I'm trying to modify the filedialog class rather than add another dialog box.

Community
  • 1
  • 1
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
  • 1
    It might be simpler to just create your own dialog. See [Dialog Windows](http://effbot.org/tkinterbook/tkinter-dialog-windows.htm) for some documentation with examples. – martineau Sep 01 '16 at 20:56
  • @martineau you're probably right, that's what I'm afraid of – C8H10N4O2 Sep 01 '16 at 20:57
  • Probably nothing too scary. For some more examples see the files `/Python/Lib/lib-tk/tkFileDialog.py` and `/Python/Lib/lib-tk/tkCommonDialog.py` on your system. Just find something that's close, make a copy, and modify it. There also a relatively simple package named [easygui](https://pypi.python.org/pypi/easygui/0.98.0) whose source also contains tkinter-based dialogs. – martineau Sep 01 '16 at 21:04

0 Answers0