473,375 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 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 7106
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

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

Similar topics

17
by: SK | last post by:
I am calling an exe thru' href, but when it executes, I get the message if I want to open the file(exe file). Is there any way I can suppress this from appearing and open the program? Thank...
5
by: Brad | last post by:
In several aspx applications I export crytal reports to pdf, xls and doc files and then the aspx page writes the selected export file to the client browser. This all works with one small quirk: ...
2
by: Matt Hamilton | last post by:
I have an application page with a datagrid with a checkbox on each row. The user should check the rows they want to modify and then click a button. When the button is clicked, I have a Click...
3
by: yup | last post by:
I was wondering if you know a way to have, let's say, you would click on a menu bar item (where something like file, edit, format, help options are on Microsoft Word) and instead of opening a...
4
by: Satya | last post by:
I am trying to display a PDF file (which I am being passed from a web service as a binary stream) in a browser, but I am being prompted to save the file instead. I don't want the user to be...
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
4
by: Charlie Brookhart | last post by:
I am trying to write a code for a button click event. When the button is clicked, it is supposed to bring up an open file dialog box to allow the user to select the document they which to open....
0
by: aarthiraaj | last post by:
In my user screen I have view link, if I click this link it will go to servlet and open one word document. It is opening properly, but while opening the attachment it generate one dialog box like...
1
navanova
by: navanova | last post by:
Greetings, I have a problem of opening ms word and excel files on my computer. The files are there for a long time. I use to open and modify them. Suddenly, when i try to open the word files, a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.