Your TableView
class needs to inherit from Frame
. When you do that, you can treat it just like any other widget.
For example:
class TableView(Frame):
def __init__(self, parent, columns, rows):
Frame.__init__(self, parent)
self.columns = columns
self.rows = rows
...
self._setup_widgets()
...
def setup_widgets(self):
self.tree = ttk.Treeview(self, columns=self.columns, show="heading")
self.tree.grid(column=0, row=0, sticky="nsew")
...
You don't need to create a container, because the object is its own container. Any child widgets within the class should be packed/gridded to self
so that they are inside the container.
Also, you shouldn't call tk.Tk() anywhere except in your main program -- that is why you get a separate window.
Your main program goes virtually unchanged, except you need to add a line of code to place the widget in the grid:
root = Tk()
root.wm_title("Main")
ttk.Button(root, text="Add new student").grid(row = 0, column = 0)
ttk.Button(root, text="Edit Student").grid(row = 0, column = 1)
ttk.Button(root, text="Edit Student").grid(row = 0, column = 2)
columns = ['ID', 'First Name', 'Last Name', 'Registration No']
rows = server.select("Student")
# create an instance of the TableView widget
tableview = TableView(root, columns, rows)
# ... and add it to the root window
tableview.grid(row=1, column=0, columnspan=3)
root.mainloop()