I just finished my first program, and now in order do expand, I'm looking for ways to improve it. And eliminate repetition and create some form of scalability. And this piece of code:
from tkinter import *
from tkinter import ttk
#Creating window, calling it root
root = Tk()
root.title("Ges_Stock")
root.geometry("1000x600")
#Creating Notebook so I can create tabs on it
my_notebook = ttk.Notebook(root)
my_notebook.pack(pady=10, fill="both")
# Tabs creation, packing, and naming it
my_frame1 = Frame(my_notebook, width=1200, height=700)
my_frame2 = Frame(my_notebook, width=1200, height=700)
my_frame3 = Frame(my_notebook, width=1200, height=700)
#my_frame4 = Frame(my_notebook, width=1200, height=700)
my_frame1.pack(fill="both", expand=1, pady=20)
my_frame2.pack(fill="both", expand=1, pady=20)
my_frame3.pack(fill="both", expand=1, pady=20)
#my_frame4.pack(fill="both", expand=1)
my_notebook.add(my_frame1, text="Barcode")
my_notebook.add(my_frame2, text="Manual Code")
my_notebook.add(my_frame3, text="Inventory Jumbo")
#my_notebook.add(my_frame4, text="Code Generator")
# Creating LabelFrames
wrapper1 = LabelFrame(my_frame1, text="Items List")
wrapper2 = LabelFrame(my_frame1, text="Add and Remove")
wrapper4 = LabelFrame(my_frame2, text="Items List")
wrapper5 = LabelFrame(my_frame2, text="Add and Remove")
wrapper7 = LabelFrame(my_frame3, text="Items List")
wrapper8 = LabelFrame(my_frame3, text="Add and Remove")
#wrapper10 = LabelFrame(my_frame4, text="Items List")
#wrapper11 = LabelFrame(my_frame4, text="Command Bar")
#Packing the frames
wrapper2.pack(fill="both", expand="yes", padx=20, ipady=20)
wrapper1.pack(fill="both", expand="yes", padx=20, ipady=20)
wrapper5.pack(fill="both", expand="yes", padx=20, ipady=20)
wrapper4.pack(fill="both", expand="yes", padx=20, ipady=20)
wrapper8.pack(fill="both", expand="yes", padx=20, ipady=20)
wrapper7.pack(fill="both", expand="yes", padx=20, ipady=20)
#wrapper11.pack(fill="both", expand="yes", padx=20, ipady=10)
#wrapper10.pack(fill="both", expand="yes", padx=20, ipady=10)
#endregion
# TREEVIEW CREATION
#Treeview on frame 1
trv = ttk.Treeview(wrapper1, columns=(1,2,3,4,5,6,7), show="headings", height="15") #
trv.pack(fill="both",padx=20, ipady=10)
#Treeview on frame 2
trv_est = ttk.Treeview(wrapper4, columns=(1, 2, 3, 4, 5, 6, 7), show="headings", height="15") #
trv_est.pack(fill="both", padx=20, ipady=10)
#Treeview on frame 3
trv_jmb = ttk.Treeview(wrapper7, columns=(1, 2, 3, 4, 5, 6, 7), show="headings", height="15") #
trv_jmb.pack(fill="both", padx=20, ipady=10)
root.mainloop()
So I decided to use classes and function to make it easier. But I'm not able to figure it out how the hierarchy works. This is what I have so far:
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('Codemy.com - Classes')
root.geometry("1000x600")
my_notebook = ttk.Notebook(root)
my_notebook.pack(pady=10, fill="both")
def btn_add():
pass
class nt_book:
def __init__(self, name_tab, num_frame):
self.nome_aba = name_tab
self.num_frame = num_frame
self.num_frame = Frame(my_notebook, width=1200, height=700)
self.num_frame.pack()
my_notebook.add(self.num_frame, text=self.nome_aba)
def wrap(self, name_wrap):
self.name_wrap = name_wrap
wrapper = LabelFrame(self.num_frame, text=self.name_wrap)
wrapper.pack(fill="both", expand="yes", padx=20, ipady=20)
# Trying to call a tab and frames inside
est = nt_book("Inventory", "frame1")
est_fr1 = est.wrap("Command Bar")
est_fr2 = est.wrap("Item List")
add_btn = Button(est_fr1, text="Add", command=btn_add)
add_btn.grid(row=1, column=1, padx=10, pady=5)
root.mainloop()
What I really want to do:
- Create a Class that adds a tab;
- Create a Class - or function - that adds labelFrames to the tabs;
- Create a Class - or function - that adds Buttons and Treeviews to the LabelFrames;
Actually I'm kind of lost on how to do it, I just don't want to copy and paste the same thing over and over again every time i want to create a new tab and add functionality to my program. I don't know if that's the case for inner classes or subclasses or even nested classes.
CodePudding user response:
Does this work for you?
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Ges_Stock")
root.geometry("1000x600")
class BetterNotebook(ttk.Notebook):
def add_frame(self, name):
frame = BetterFrame(self, width=1200, height=700)
frame.pack(fill="both", expand=1, pady=20)
self.add(frame, text=name)
class BetterFrame(Frame):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.add_remove_frame = LabelFrame(self, text="Add and Remove")
self.add_remove_frame.pack(fill="both", expand="yes", padx=20, ipady=20)
self.treeview_frame = LabelFrame(self, text="Item List")
self.treeview_frame.pack(fill="both", expand="yes", padx=20, ipady=20)
trv = ttk.Treeview(self.treeview_frame, columns=(1,2,3,4,5,6,7), show="headings", height="15")
trv.pack(fill="both",padx=20, ipady=10)
my_notebook = BetterNotebook(root)
my_notebook.pack(pady=10, fill="both")
for x in ["Barcode", "Manual Code", "Inventory Jumbo"]:
my_notebook.add_frame(x)
if __name__ == "__main__":
root.mainloop()