I have the code below which is based on this question here: Expandable and contracting frame in Tkinter
from tkinter import *
from tkinter import ttk
class Example:
def __init__(self, root):
list_1 = ['item_1', 'item_2']
list_2 = ['a', 'b', 'c']
for item in list_1 :
frame = Frame(root, width=500, height=22)
frame.pack(side=TOP, anchor=W)
frame.propagate(0)
Label(frame, text=item).pack(side=LEFT, anchor=W)
entry = Entry(frame, width=11)
entry.pack(side=RIGHT, pady=2, anchor=E)
var = IntVar()
var.set(0)
sub_frame = Frame(root, relief="sunken", width=400, height=22, borderwidth=1)
toggle_btn = ttk.Checkbutton(frame, width=2, text='+', command=lambda: self.toggle(var, toggle_btn, sub_frame),
variable=var, style='Toolbutton')
toggle_btn.pack(side=LEFT, anchor=E)
for item in list_2:
Label(sub_frame, text=item).pack(side=TOP)
def toggle(self, show, toggle_button, sub_frame):
if bool(show.get()):
sub_frame.pack(fill="x", expand=1)
sub_frame.propagate(1)
toggle_button.configure(text='-')
else:
sub_frame.forget()
toggle_button.configure(text='+')
def main():
root = Tk()
open = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
I want here to create two expandable frames from list_1 and fill each with labels from list_2. The two frames should then be expandable and collapsible to show and hide the items from list_2.
The second frame functions correctly, but the first doesn't because it was overwritten by the second frame.
So the code initially gives you this:
If you click the '+' next to item_1, nothing happens.
If you click the '+' next to item_2, this happens.
By clicking on the '+' next to item_1, I want the sub_frame to be shown underneath item_1(between item_1 and item_2), just like clicking on the '+' next to item_2 shows the sub_frame underneath it.
Any help on how to get both frames to work correctly? I've read about storing the frames created in a list, however I couldn't get this to work for me.
Many thanks