Skip to content

Commit

Permalink
ttk.Notebook for tabbed display of multiple Presenters
Browse files Browse the repository at this point in the history
The system is slightly hacky, because the Presenter is not independent from the
main window - indeed, it knows that it has to place itself in a root's notebook
as opposed to *being* placed by the Main object. It's a fairly simple mechanism
though: the root constructs a ttk.Notebook, builds a Presenter and orders it to
place itself in a given tab - the Presenter knows that it is supposed to occupy
a single whole tab (and there will be nothing else besides it in it), so it can
grid itself simply at (0,0). Then it just calls `add` on the root's notebook to
create the tab.
By the way, the Main window construction was slightly refactored: "__construct"
does not make much sense now that everything is built by the child objects, and
the only thing Main does besides providing space for them is building a control
panel.
  • Loading branch information
Noiredd committed Nov 20, 2018
1 parent 839cf74 commit 3c26b88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
37 changes: 18 additions & 19 deletions fw-local/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,38 @@ class Main(object):

def __init__(self):
self.root = root = tk.Tk()
#prepare the components
self.loginHandler = Login(self.root)
#prepare the window
root.title('FW local')
root.resizable(False, False)
self.__construct()
#construct the window: first the notebook for tabbed view
self.notebook = ttk.Notebook(root)
self.notebook.grid(row=0, column=0, padx=5, pady=5, sticky=tk.NW)
#control panel
frame = tk.Frame(root)
frame.grid(row=1, column=0, padx=5, pady=5, sticky=tk.SW)
ttk.Button(frame, text='Aktualizuj', command=self._updateData).grid(row=0, column=0, sticky=tk.SW)
ttk.Button(frame, text='PRZEŁADUJ!', command=self._reloadData).grid(row=0, column=1, sticky=tk.SW)
self.progressVar = tk.IntVar()
self.progressbar = ttk.Progressbar(root, orient='horizontal', length=400, mode='determinate', variable=self.progressVar)
self.progressbar.grid(row=1, column=0, padx=5, pady=5)
self._setProgress(-1) # start with the progress bar hidden
ttk.Button(root, text='Wyjście', command=self._quit).grid(row=1, column=0, padx=5, pady=5, sticky=tk.SE)
#load the savefile and instantiate Presenter(s) and Database(s)
self.databases = []
self.presenters = []
userdata = self.loadUserData()
u_name = userdata['username'] if userdata else ''
conf_m = userdata['movie_cfg'] if userdata else ''
data_m = userdata['movie_db'] if userdata else ''
self.loginHandler = Login(self.root)
self.api = FilmwebAPI(self.loginHandler.requestLogin, u_name)
movieDatabase = Database.restoreFromString('Movie', data_m, self.api, self._setProgress)
moviePresenter = Presenter(root, self.api, movieDatabase, conf_m)
moviePresenter.grid(row=0, column=0, rowspan=3, columnspan=3, padx=5, pady=5, sticky=tk.NW)
moviePresenter = Presenter(self, self.api, movieDatabase, conf_m)
moviePresenter.addFilter(filters.YearFilter, row=0, column=0, sticky=tk.NW)
moviePresenter.addFilter(filters.RatingFilter, row=1, column=0, sticky=tk.NW)
moviePresenter.addFilter(filters.DateFilter, row=2, column=0, sticky=tk.NW)
moviePresenter.addFilter(filters.GenreFilter, row=0, column=1, rowspan=3, sticky=tk.NW)
moviePresenter.addFilter(filters.CountryFilter, row=0, column=2, rowspan=3, sticky=tk.NW)
moviePresenter.addFilter(filters.DirectorFilter, row=0, column=3, rowspan=3, sticky=tk.NW)
moviePresenter.placeInTab('Filmy')
moviePresenter.totalUpdate()
self.databases.append(movieDatabase)
self.presenters.append(moviePresenter)
Expand All @@ -153,20 +162,10 @@ def __init__(self):
root.protocol('WM_DELETE_WINDOW', self._quit)
if not userdata:
self._reloadData() # first run
#prevent resizing and run the app
root.resizable(False, False)
tk.mainloop()

#CONSTRUCTION
def __construct(self):
#control panel
frame = tk.Frame(self.root)
ttk.Button(frame, text='Aktualizuj', command=self._updateData).grid(row=0, column=0, sticky=tk.SW)
ttk.Button(frame, text='PRZEŁADUJ!', command=self._reloadData).grid(row=0, column=1, sticky=tk.SW)
self.progressVar = tk.IntVar()
self.progressbar = ttk.Progressbar(self.root, orient='horizontal', length=400, mode='determinate', variable=self.progressVar)
self.progressbar.grid(row=4, column=1, padx=5, pady=5)
self._setProgress(-1) # start with the progress bar hidden
tk.Button(self.root, text='Wyjście', command=self._quit).grid(row=4, column=2, padx=5, pady=5, sticky=tk.SE)
frame.grid(row=4, column=0, padx=5, pady=5, sticky=tk.S+tk.W)
def centerWindow(self):
self.root.update()
ws = self.root.winfo_screenwidth()
Expand Down
15 changes: 7 additions & 8 deletions fw-local/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class Presenter(object):
"""
def __init__(self, root, api, database, config:str, displayRating=True):
self.root = root
self.main = tk.Frame(root)
self.main = ttk.Frame(root.notebook)
self.database = database
self.items = []
self.config = Config.restoreFromString(database.itemtype, config, self)
Expand Down Expand Up @@ -355,7 +355,7 @@ def __construct(self):
self.stats = StatView(self.main, self.database.itemtype)
self.stats.grid(row=0, column=2, sticky=tk.NW)
# FILTER FRAME
self.fframe = tk.Frame(self.main)
self.fframe = ttk.Frame(self.main)
self.fframe.grid(row=1, column=2, sticky=tk.NW)
# store the row and col range of inserted filters to know where to place the
# reset all button
Expand Down Expand Up @@ -385,12 +385,6 @@ def __placeResetAllButton(self):
def storeToString(self):
return self.config.storeToString()

# TK interface for GUI placement
def pack(self, **kw):
self.main.pack(**kw)
def grid(self, **kw):
self.main.grid(**kw)

def addFilter(self, filter_class, **grid_args):
filter_object = filter_class(self.fframe)
self.filtMachine.registerFilter(filter_object)
Expand All @@ -401,6 +395,11 @@ def addFilter(self, filter_class, **grid_args):
if grid_args['column'] > self.fframe_grid[1]:
self.fframe_grid[1] = grid_args['column']

# take a spot in the entire Notebook tab of a given name
def placeInTab(self, tab):
self.main.grid(row=0, column=0, pady=5, padx=5)
self.root.notebook.add(self.main, text=tab)

# Display pipeline
# Internally, the pipeline consists of 4 steps: acquiring data from the DB,
# filtering it using the given filters, sorting by the given criterion, and
Expand Down

0 comments on commit 3c26b88

Please sign in to comment.