Basic File Browse

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












Below is a Browse class that lets the user browse files and show the path in an entry frame. This is pretty short and basic.



import tkinter as tk
from tkinter import filedialog as fd


class Browse(tk.Frame):
""" Creates a frame that contains a button when clicked lets the user to select
a file and put its filepath into an entry.
"""

def __init__(self, master, initialdir='', filetypes=()):
super().__init__(master)
self.filepath = tk.StringVar()
self._initaldir = initialdir
self._filetypes = filetypes
self._create_widgets()
self._display_widgets()

def _create_widgets(self):
self._entry = tk.Entry(self, textvariable=self.filepath)
self._button = tk.Button(self, text="Browse...", command=self.browse)

def _display_widgets(self):
self._entry.pack(fill='x', expand=True)
self._button.pack(anchor='se')

def browse(self):
""" Browses a .png file or all files and then puts it on the entry.
"""

self.filepath.set(fd.askopenfilename(initialdir=self._initaldir,
filetypes=self._filetypes))


if __name__ == '__main__':
root = tk.Tk()

file_browser = Browse(root, initialdir=r"C:Users",
filetypes=(('Portable Network Graphics','*.png'),
("All files", "*.*")))
file_browser.pack(fill='x', expand=True)

root.mainloop()


Review Concern(s):



  • My main concern is to code in an easy to read, efficient, and well-structured manner while still learning the language and concepts such as OOP. Feel free to mention the tiniest issue or improvement that comes to your mind, as I am a beginner and I probably need it.






share|improve this question



























    up vote
    4
    down vote

    favorite












    Below is a Browse class that lets the user browse files and show the path in an entry frame. This is pretty short and basic.



    import tkinter as tk
    from tkinter import filedialog as fd


    class Browse(tk.Frame):
    """ Creates a frame that contains a button when clicked lets the user to select
    a file and put its filepath into an entry.
    """

    def __init__(self, master, initialdir='', filetypes=()):
    super().__init__(master)
    self.filepath = tk.StringVar()
    self._initaldir = initialdir
    self._filetypes = filetypes
    self._create_widgets()
    self._display_widgets()

    def _create_widgets(self):
    self._entry = tk.Entry(self, textvariable=self.filepath)
    self._button = tk.Button(self, text="Browse...", command=self.browse)

    def _display_widgets(self):
    self._entry.pack(fill='x', expand=True)
    self._button.pack(anchor='se')

    def browse(self):
    """ Browses a .png file or all files and then puts it on the entry.
    """

    self.filepath.set(fd.askopenfilename(initialdir=self._initaldir,
    filetypes=self._filetypes))


    if __name__ == '__main__':
    root = tk.Tk()

    file_browser = Browse(root, initialdir=r"C:Users",
    filetypes=(('Portable Network Graphics','*.png'),
    ("All files", "*.*")))
    file_browser.pack(fill='x', expand=True)

    root.mainloop()


    Review Concern(s):



    • My main concern is to code in an easy to read, efficient, and well-structured manner while still learning the language and concepts such as OOP. Feel free to mention the tiniest issue or improvement that comes to your mind, as I am a beginner and I probably need it.






    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Below is a Browse class that lets the user browse files and show the path in an entry frame. This is pretty short and basic.



      import tkinter as tk
      from tkinter import filedialog as fd


      class Browse(tk.Frame):
      """ Creates a frame that contains a button when clicked lets the user to select
      a file and put its filepath into an entry.
      """

      def __init__(self, master, initialdir='', filetypes=()):
      super().__init__(master)
      self.filepath = tk.StringVar()
      self._initaldir = initialdir
      self._filetypes = filetypes
      self._create_widgets()
      self._display_widgets()

      def _create_widgets(self):
      self._entry = tk.Entry(self, textvariable=self.filepath)
      self._button = tk.Button(self, text="Browse...", command=self.browse)

      def _display_widgets(self):
      self._entry.pack(fill='x', expand=True)
      self._button.pack(anchor='se')

      def browse(self):
      """ Browses a .png file or all files and then puts it on the entry.
      """

      self.filepath.set(fd.askopenfilename(initialdir=self._initaldir,
      filetypes=self._filetypes))


      if __name__ == '__main__':
      root = tk.Tk()

      file_browser = Browse(root, initialdir=r"C:Users",
      filetypes=(('Portable Network Graphics','*.png'),
      ("All files", "*.*")))
      file_browser.pack(fill='x', expand=True)

      root.mainloop()


      Review Concern(s):



      • My main concern is to code in an easy to read, efficient, and well-structured manner while still learning the language and concepts such as OOP. Feel free to mention the tiniest issue or improvement that comes to your mind, as I am a beginner and I probably need it.






      share|improve this question













      Below is a Browse class that lets the user browse files and show the path in an entry frame. This is pretty short and basic.



      import tkinter as tk
      from tkinter import filedialog as fd


      class Browse(tk.Frame):
      """ Creates a frame that contains a button when clicked lets the user to select
      a file and put its filepath into an entry.
      """

      def __init__(self, master, initialdir='', filetypes=()):
      super().__init__(master)
      self.filepath = tk.StringVar()
      self._initaldir = initialdir
      self._filetypes = filetypes
      self._create_widgets()
      self._display_widgets()

      def _create_widgets(self):
      self._entry = tk.Entry(self, textvariable=self.filepath)
      self._button = tk.Button(self, text="Browse...", command=self.browse)

      def _display_widgets(self):
      self._entry.pack(fill='x', expand=True)
      self._button.pack(anchor='se')

      def browse(self):
      """ Browses a .png file or all files and then puts it on the entry.
      """

      self.filepath.set(fd.askopenfilename(initialdir=self._initaldir,
      filetypes=self._filetypes))


      if __name__ == '__main__':
      root = tk.Tk()

      file_browser = Browse(root, initialdir=r"C:Users",
      filetypes=(('Portable Network Graphics','*.png'),
      ("All files", "*.*")))
      file_browser.pack(fill='x', expand=True)

      root.mainloop()


      Review Concern(s):



      • My main concern is to code in an easy to read, efficient, and well-structured manner while still learning the language and concepts such as OOP. Feel free to mention the tiniest issue or improvement that comes to your mind, as I am a beginner and I probably need it.








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 9 at 0:01
























      asked Jan 8 at 15:12









      Nae

      25019




      25019




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I think it looks pretty good.



          Personally I wouldn't use a StringVar, unless part of the goal was to allow the caller to link this variable to some other widget. Often, the use of StringVar just adds another object to keep track of without providing much of a benefit in return, since you can easily set the value of an entry or label widget without it.



          I also personally find it a bit easier to define the file types as a variable since it can be cumbersome to squeeze a bunch of file types into the list of parameters. In this case it's not too bad, but you might want to consider doing it like this:



          filetypes = (
          ('Portable Network Graphics','*.png'),
          ("All files", "*.*")
          )
          file_browser = Browse(root, initialdir=r"C:Users",
          filetypes=filetypes)





          share|improve this answer





















          • Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
            – Nae
            Jan 8 at 22:49










          • That makes sense.
            – Bryan Oakley
            Jan 8 at 23:00










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184589%2fbasic-file-browse%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          I think it looks pretty good.



          Personally I wouldn't use a StringVar, unless part of the goal was to allow the caller to link this variable to some other widget. Often, the use of StringVar just adds another object to keep track of without providing much of a benefit in return, since you can easily set the value of an entry or label widget without it.



          I also personally find it a bit easier to define the file types as a variable since it can be cumbersome to squeeze a bunch of file types into the list of parameters. In this case it's not too bad, but you might want to consider doing it like this:



          filetypes = (
          ('Portable Network Graphics','*.png'),
          ("All files", "*.*")
          )
          file_browser = Browse(root, initialdir=r"C:Users",
          filetypes=filetypes)





          share|improve this answer





















          • Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
            – Nae
            Jan 8 at 22:49










          • That makes sense.
            – Bryan Oakley
            Jan 8 at 23:00














          up vote
          1
          down vote



          accepted










          I think it looks pretty good.



          Personally I wouldn't use a StringVar, unless part of the goal was to allow the caller to link this variable to some other widget. Often, the use of StringVar just adds another object to keep track of without providing much of a benefit in return, since you can easily set the value of an entry or label widget without it.



          I also personally find it a bit easier to define the file types as a variable since it can be cumbersome to squeeze a bunch of file types into the list of parameters. In this case it's not too bad, but you might want to consider doing it like this:



          filetypes = (
          ('Portable Network Graphics','*.png'),
          ("All files", "*.*")
          )
          file_browser = Browse(root, initialdir=r"C:Users",
          filetypes=filetypes)





          share|improve this answer





















          • Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
            – Nae
            Jan 8 at 22:49










          • That makes sense.
            – Bryan Oakley
            Jan 8 at 23:00












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I think it looks pretty good.



          Personally I wouldn't use a StringVar, unless part of the goal was to allow the caller to link this variable to some other widget. Often, the use of StringVar just adds another object to keep track of without providing much of a benefit in return, since you can easily set the value of an entry or label widget without it.



          I also personally find it a bit easier to define the file types as a variable since it can be cumbersome to squeeze a bunch of file types into the list of parameters. In this case it's not too bad, but you might want to consider doing it like this:



          filetypes = (
          ('Portable Network Graphics','*.png'),
          ("All files", "*.*")
          )
          file_browser = Browse(root, initialdir=r"C:Users",
          filetypes=filetypes)





          share|improve this answer













          I think it looks pretty good.



          Personally I wouldn't use a StringVar, unless part of the goal was to allow the caller to link this variable to some other widget. Often, the use of StringVar just adds another object to keep track of without providing much of a benefit in return, since you can easily set the value of an entry or label widget without it.



          I also personally find it a bit easier to define the file types as a variable since it can be cumbersome to squeeze a bunch of file types into the list of parameters. In this case it's not too bad, but you might want to consider doing it like this:



          filetypes = (
          ('Portable Network Graphics','*.png'),
          ("All files", "*.*")
          )
          file_browser = Browse(root, initialdir=r"C:Users",
          filetypes=filetypes)






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jan 8 at 22:43









          Bryan Oakley

          1,494712




          1,494712











          • Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
            – Nae
            Jan 8 at 22:49










          • That makes sense.
            – Bryan Oakley
            Jan 8 at 23:00
















          • Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
            – Nae
            Jan 8 at 22:49










          • That makes sense.
            – Bryan Oakley
            Jan 8 at 23:00















          Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
          – Nae
          Jan 8 at 22:49




          Thanks for your review. I actually didn't initially use a StringVar too, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of the entry with keyboard self.filename would be in sync. I thought it would be misguiding to give the user an entry that they can edit and then have no account for that edit.
          – Nae
          Jan 8 at 22:49












          That makes sense.
          – Bryan Oakley
          Jan 8 at 23:00




          That makes sense.
          – Bryan Oakley
          Jan 8 at 23:00












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184589%2fbasic-file-browse%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods