Basic File Browse

Clash 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.
python object-oriented python-3.x file tkinter
add a comment |Â
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.
python object-oriented python-3.x file tkinter
add a comment |Â
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.
python object-oriented python-3.x file tkinter
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.
python object-oriented python-3.x file tkinter
edited Jan 9 at 0:01
asked Jan 8 at 15:12
Nae
25019
25019
add a comment |Â
add a comment |Â
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)
Thanks for your review. I actually didn't initially use aStringVartoo, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of theentrywith keyboardself.filenamewould 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
add a comment |Â
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)
Thanks for your review. I actually didn't initially use aStringVartoo, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of theentrywith keyboardself.filenamewould 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
add a comment |Â
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)
Thanks for your review. I actually didn't initially use aStringVartoo, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of theentrywith keyboardself.filenamewould 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
add a comment |Â
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)
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)
answered Jan 8 at 22:43
Bryan Oakley
1,494712
1,494712
Thanks for your review. I actually didn't initially use aStringVartoo, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of theentrywith keyboardself.filenamewould 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
add a comment |Â
Thanks for your review. I actually didn't initially use aStringVartoo, based on your suggestions in a question. Then I wanted to use it so that if the user manipulated the content of theentrywith keyboardself.filenamewould 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password