Opening files with file picker - PyQt with Python GUI  Programming tutorial 14

Opening files with file picker – PyQt with Python GUI Programming tutorial 14



Views:64897|Rating:4.92|View Time:6:5Minutes|Likes:364|Dislikes:6
In this PyQT4 GUI application development tutorial, we cover how to open a file in our editor application with the file picker widget.

sample code:

what is going on everybody welcome to part 14 of our Python with PI QT for GUI application development tutorial video series in this video we're going to be talking about how can we open up a file so it turns out that built within QT is a file picker so to speak where we can pull it up and actually choose a file from our operating systems kind of directories so that's pretty cool because it's built right in to QT so let's just go ahead and jump in because how can we have an editor if we don't have a file to edit that doesn't make any sense so what we're gonna do is we're gonna add another bit like we've got open editor well also we want to have is like file open so we want to be able to go like open file for example and we'll say that is equal to and in fact let's just copy and paste all of this that would make a lot of sense and and then just start renaming stuff so open file copy paste paste paste instead of control e that would make sense to be control o we're gonna make this open file and then status tip can be open file and then the triggered method will be file underscore open and that should be it the only other thing of course we need to add it to the file menu so what we'll do is file menu dot add action and we want to add open file so open file awesome now we need to add our fight or what do we call that method file open let's just copy and paste forget it scrolling on down we add another method what's oh dear we always made a huge mess of the place uh we were gonna do it I don't know let's do it right here so define file actually spaced yeah file open and then it was past self and now you're ready to go so first of all we have a name in a file so first we'll say the name here is going to be equal to Q T GUI equals q TQ e dot Q file dialog dot get open file name self and then this will be the title basically to the little window that pops up and we'll title it open file then we're gonna say file equals open whatever that name is and we're gonna open it with the intention to read okay so then so when you when we open this file what are we opening the file for so we can open it and we can read it in or well this is just opening it with the intention to read we haven't actually read a thing but it so the nails let's go even past that so we know we want to open the file what do we want to do with it well we want to load it into that text editor and then up here how do we get that file name this just it's the file dialog built in the Qt and whatever file we pick that's being returned as the file name done so anyway moving right along after we have the file name what do we want to do well first of all we're gonna call self dot editor why are we calling self dot editor we need the editor to be there if you recall the editor is not there by default right we were going like show editor so if we don't call self dot editor it won't be there the file will load and we can even populate the editor with the stuff but until we call you know to set it as a central widget and all that now it won't actually show up anywhere so anyways now we'll say with file we're gonna say text equals file dot read and then we're gonna say self dot text edit dot set next text and that should be all we need text edit you just make sure even was texted I don't even know we're gonna run this and see how it works okay so first of all I want change to may be plastique so pretty now first let's just open the editor cool so the editor works now let's try open file and then here are the list of videos that we're writing right now how about we open the same one that we're making right now do it here it is lovely lovely so there is our file that's pretty cool if these things excite me whenever you finally that get to this point and you're like wow that's awesome it worked anyway so that's cool and it's a pretty good-looking editor I mean we have this babyface I mean that's nice anyway but just just just admire the beauty of Pi QT all the things that we have so far and this is a pretty darn good-looking window you just want to have a good looking window like this if you had tkinter so anyways now we have the editor we can open a file and again you've got this nice file picker that is like already there for you it's easy easy to use and all that so anyways let's close out of this and now the next and final thing that we really need to do to finish up this little editing application that we've built for ourselves is we need to be able to save the file to whatever we want so this we're going to be talking about in the next video so if you have any questions or comments up to this point please feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time

30 Replies to “Opening files with file picker – PyQt with Python GUI Programming tutorial 14”

  1. Jon-Gavin Harris

    I know this is a bit old, but I was wondering how to exit the editor once you're done editing? I would like to be able to view a data set, and then close the editor and return to the app, Thank you

  2. aashima gupta

    Hi buddy,
    I really want to go with a simple approach and i am finding it tricky.
    So, i have a set of python files in a folder and now i want a presentable GUi to run them. Also,
    i want that on a click of button a particular python file should start running automatically Accepting the data from text field, without having to run them from command prompt.
    How should i go for it??
    I have Python 2.7 so which PyQt should i choose??
    Please Help

  3. exosoul

    I get an error when opening the file.  Can someone please help?  Error below:

    Traceback (most recent call last):
        File "file location here.py", line 101, in file_open
            file = open(name, 'r')
    TypeError: coercing to Unicode: need string or buffer, tuple found

  4. Carl Franz

    Still. We have a font selector which does not do anything to the text in the editor. So, we have a bunch of really cool stuff which does not integrate.

  5. Tech Minded

    When I go to open the file, it returns an error saying my file is an invalid file type. Does anyone know how to fix this? Thanks.

  6. Gtrader Ng

    In case some of you cannot read the text file by your editor, it may due to the
    QtGui.QFileDialog.getOpenFileName(self, 'Open File')
    returns a tuple of 2 elements:
    name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
    return
    (u'C:/Users/username/filename.sth', u'All Files (.)')
    so you may change the line

    file = open(name, 'r')

    to

    file = open(name[0], 'r')

    This should work. I am using python 2.7

  7. Ana Amorim

    Hi! Cool channel very useful! I am having a lot of trouble with the module wishbone for python, have you ever come across it? There is a python GUI now available for Wishbone but I cannot open it and I dont know even how, the code is $> wishbone_gui.py . Thanks Best Ana

  8. Gabe Payne

    I'm trying to build a twitter application where you can either choose to update your status with text or with a picture file. I have the command version of this working perfectly, I just want to turn it into a gui. Is this tutorial in the right direction? I want to be able to browse to an image only and then tweet it.

  9. Tonyo

    How do you limit the file types that the file dialog can see to certain file types? Like for example I want the file dialog to only see '.xml' files. How do I do that? Also, how do I handle when the cancel button was clicked? I assume you could maybe use assert, but is there anything else that is better to use?

  10. Xingyu Bian

    File "C:Python34gui.py", line 186, in file_open
        self.textEdit.setText(text)
    AttributeError: 'Window' object has no attribute 'textEdit'

  11. pstoreyc

    Great videos – super helpful for learning. I'm wondering if there's one in which you show how to open a whole new window from the main window I.e. Like a kind of wizard you can run from the main application?

  12. SphelixDesigns

    What module would you say is most appropriate for advanced GUI for Python. I have only used Tkinter and although the ttk makes the application more appealing and better in terms of visual, i wish to add my own style towards it. Personally i was thinking kivy but i'm not sure. An example of what i want is like the 'TutorialsPoint' website where i have the menu on the left and the information to the right… If you understand that 🙂

  13. Eleanor May

    Hi,
    Is there a way in python that will let you write code in your program that will clear the screen after a number of seconds? Thanks.

  14. salma cherif

    Hey , Thank you for this awesome tutorial. it has been helping me alot through my project. Could you tell me how to catch the name of the file we select ?
    Could you please be so nice as to pass me a link for the different functions and libraries of PyQt with their usage samples? That would be much of a help for me !

  15. 郭宇

    Hi sentdex, thank you for your PyQt series. I hope you can give a sub-series about Qt Designer comes with PyQt installation. Coding entire GUI using just IDE makes it hard to track and change locations of every object. Don't know if Qt Designer is like a WYSIWYG tool and able to create each GUI object as a Python 'Object'.

  16. renzo cabarios

    Hi sentdex can you make a series about you making python programs like basic cwlculator , a text based game or anything, because i tink that people really want to know what can python can do and i think you are the one whos gonna show us pls consider my suggestion.

  17. thechemist90

    Hi sentdex,

    I really enjoy your videos. They have been really helpful thus far as I learn PyQt. I was wondering if you could show how to implement the grid layout in the script that you have been writing in this series because by using the "move" widget option, my widgets show at a different position than intended when looking at my program at a resolution other than 1080. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *