471,334 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Opening a file dialog

Hello,
i have a program in which i plot 10 subplots with matplotlib. To launch the program i now type in the console:
python dataplot.py DATAFILE_10.10.07.csv
However what i want is that python asks me for a file which i can then select in a sort of browse window. How to implement this?
I though of creating a GUI with Qt in which you browse (which i dont know how to do) and then click on the button PLOT which should launch the program dataplot with the datafile that has ben selected.
Hope someone can help me.
Oct 26 '07 #1
3 6903
bartonc
6,596 Expert 4TB
Hello,
i have a program in which i plot 10 subplots with matplotlib. To launch the program i now type in the console:
python dataplot.py DATAFILE_10.10.07.csv
However what i want is that python asks me for a file which i can then select in a sort of browse window. How to implement this?
I though of creating a GUI with Qt in which you browse (which i dont know how to do) and then click on the button PLOT which should launch the program dataplot with the datafile that has ben selected.
Hope someone can help me.
It's pretty simple using Tkinter. There's even a way to hide the Tk main window (I think):
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import Tk
  2. from tkFileDialog import askopenfilename
  3.  
  4.  
  5. if __name__ == '__main__':
  6.     root=Tk()
  7.     fileName = askopenfilename()
  8.     root.destroy()
  9.     print fileName
  10.  
Oct 26 '07 #2
dshimer
136 Expert 100+
When working in windows, I use the code that I have pasted below. At one time I looked at all the interesting things you could do with this simple example, but have just been copying, pasting, and making minor mods for years. I have to say though that within win32 it is about as simple and straight forward as I could find.
Expand|Select|Wrap|Line Numbers
  1. >>> import win32ui,win32con
  2. >>> fd = win32ui.CreateFileDialog(1,'','', 0,'Text |*.txt')
  3. >>> fd.SetOFNTitle("Select Text File")
  4. >>> if fd.DoModal() == win32con.IDCANCEL: sys.exit(1)
  5. ... 
  6. >>> print fd.GetPathName().replace('\\','/')
  7. C:/users/dennis/python/forum.txt
  8. >>> 
If I want to select multiple files and have the names returned as a list I would do something like.
Expand|Select|Wrap|Line Numbers
  1. import win32ui,win32con,sys
  2. fd = win32ui.CreateFileDialog(1,None,None,win32con.OFN_ALLOWMULTISELECT,'All Files|*.*')
  3. fd.SetOFNInitialDir('c:\\tmp')
  4. fd.SetOFNTitle('Select multiple files')
  5. if fd.DoModal() == win32con.IDCANCEL:sys.exit(1)
  6. print fd.GetPathNames()
Which if I run it and select some random files gives me something like...
Expand|Select|Wrap|Line Numbers
  1. ['C:\\tmp\\Stadium.png', 'C:\\tmp\\BucksX4.png', 'C:\\tmp\\ShiftProblem.png', 'C:\\tmp\\BucksX1.png']
Oct 31 '07 #3
bartonc
6,596 Expert 4TB
When working in windows, I use the code that I have pasted below. At one time I looked at all the interesting things you could do with this simple example, but have just been copying, pasting, and making minor mods for years. I have to say though that within win32 it is about as simple and straight forward as I could find.
Expand|Select|Wrap|Line Numbers
  1. >>> import win32ui,win32con
  2. >>> fd = win32ui.CreateFileDialog(1,'','', 0,'Text |*.txt')
  3. >>> fd.SetOFNTitle("Select Text File")
  4. >>> if fd.DoModal() == win32con.IDCANCEL: sys.exit(1)
  5. ... 
  6. >>> print fd.GetPathName().replace('\\','/')
  7. C:/users/dennis/python/forum.txt
  8. >>> 
If I want to select multiple files and have the names returned as a list I would do something like.
Expand|Select|Wrap|Line Numbers
  1. import win32ui,win32con,sys
  2. fd = win32ui.CreateFileDialog(1,None,None,win32con.OFN_ALLOWMULTISELECT,'All Files|*.*')
  3. fd.SetOFNInitialDir('c:\\tmp')
  4. fd.SetOFNTitle('Select multiple files')
  5. if fd.DoModal() == win32con.IDCANCEL:sys.exit(1)
  6. print fd.GetPathNames()
Which if I run it and select some random files gives me something like...
Expand|Select|Wrap|Line Numbers
  1. ['C:\\tmp\\Stadium.png', 'C:\\tmp\\BucksX4.png', 'C:\\tmp\\ShiftProblem.png', 'C:\\tmp\\BucksX1.png']
A great solution, D. It's nice to know that you are still around.
Oct 31 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Matt Hamilton | last post: by
2 posts views Thread by OutdoorGuy | last post: by
4 posts views Thread by Charlie Brookhart | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.