0

I am trying to create a GUI using the first answer here. I am running into some issues because I don't fully understand how everything should be connected.

I am using a class to create a grid of boxes. Each box uses bind to call some function when clicked.

  • Where do I create the Many_Boxes class? see all the way at the bottom for an example of what I'm trying to do.

  • In the Many_Boxes class that is in the Main class I have a actions that I bind to a function. Where do I put that function? How do I call that function? What if I want to call that function from the Nav class?

I have:

class Nav(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent


class Main(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.hand_grid_dict = self.create_hand_grid()

    class Many_Boxes:
        <<<<<<<<<  bunch of code here  >>>>>>>>>>>>>>
        self.hand_canvas.bind("<Button-1>", lambda event: button_action(canvas_hand))  <<<<<< WHAT DO I NAME THIS??? self.parent.....?





class MainApplication(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)


        self.navbar = Nav(self)
        self.main = Main(self)


        self.navbar.pack(side="left", fill="y")
        self.main.pack(side="right", fill="both", expand=True)



if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).grid(row=1, column=1)
    root.mainloop()

Where do I put this:

 def create_grid(self):
        for x in y:
            your_box = self.Many_Boxes(.......)
lessharm
  • 65
  • 13

1 Answers1

0

If I'm understanding your question correctly, there is no need to create a class just to create the boxes. All you have to do is replace the Many_Boxes class with your create_hand_grid function, and define the button_action function:

import tkinter as tk

class Navbar(tk.Frame):
    def __init__(self, parent):
        self.parent = parent
        super().__init__(self.parent)

        tk.Label(self.parent, text='Navbar').pack()

        tk.Button(self.parent, text='Change Color', command=self.change_color).pack()

    def change_color(self):
        # access upwards to MainApp, then down through Main, then ManyBoxes
        self.parent.main.many_boxes.boxes[0].config(bg='black')

class ManyBoxes(tk.Frame):
    def __init__(self, parent):
        self.parent = parent
        super().__init__(self.parent)

        self.boxes = []
        self.create_boxes()

    def button_action(self, e):
        print('%s was clicked' % e.widget['bg'])

    def create_boxes(self):
        colors = ['red', 'green', 'blue', 'yellow']
        c = 0
        for n in range(2):
            for m in range(2):
                box = tk.Frame(self, width=100, height=100, bg=colors[c])
                box.bind('<Button-1>', self.button_action)
                box.grid(row=n, column=m)
                self.boxes.append(box)
                c += 1

class Main(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.many_boxes = ManyBoxes(self)
        self.many_boxes.pack()

class MainApp(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.navbar = Navbar(self)
        self.navbar.pack(fill=tk.Y)

        self.main = Main(self)
        self.main.pack(fill=tk.BOTH, expand=True)

if __name__ == "__main__":
    root = tk.Tk()
    MainApp(root).pack()
    root.mainloop()

I've filled in create_hand_grid and button_action so you can copy-paste the code and see it work.

Nelson
  • 922
  • 1
  • 9
  • 23
  • Thanks for the answer but there is a lot more going on with the Many_Boxes class that I didn't include in the question to make it easier to read. I have a GUI thats getting really complicated so I am trying to put everything into classes but I dont get the logic of how to call different things. Lets say I had a button in the Nav bar that changes a bunch of the Many_Box objects. How to I create the button? is it self.parent.main.... or what? – lessharm Jul 20 '17 at 14:46
  • @lessharm I think I understand what you want now. I've updated the code – Nelson Jul 20 '17 at 22:16
  • Thanks, that's really helpful :D – lessharm Jul 20 '17 at 22:37