0

update the number of objects using a scale

 prisoners_num_frame = ttk.Frame(setting_frame, bootstyle=LIGHT)   
    ttk.Label(prisoners_num_frame,
              text="# of prisoners",
              font=(DEFAULT_FONT, DEFAULT_FONT_SIZE),
              bootstyle=(LIGHT, INVERSE))\
      .pack(side=LEFT, padx=DEFAULT_PADDING, pady=DEFAULT_PADDING, anchor=W)
    prisoners_num_label = ttk.Label(prisoners_num_frame,
                                    text = self.numberOfPrisoners.get(),
                                    font=(DEFAULT_FONT, DEFAULT_FONT_SIZE),
                                    bootstyle=(LIGHT, INVERSE))
    prisoners_num_label.pack(padx=DEFAULT_PADDING, pady=DEFAULT_PADDING, side=RIGHT)
    ttk.Scale(prisoners_num_frame,
              from_=2,
              to=150,
              variable=self.numberOfPrisoners,
              command=_setNumberOfPrisonersLabel,
              bootstyle=DARK,
              style='TScale')\
      .pack(side=RIGHT, padx=DEFAULT_PADDING, pady=DEFAULT_PADDING, anchor=E)
    prisoners_num_frame.pack()

create the matrix of objects

 def _drawBoxMatrix(self):
    img = PhotoImage(file='./src/images/open-box.png')
    self.matrix_frame = ttk.Frame(self.root, bootstyle=LIGHT)
    ttk.Label(self.matrix_frame, text="Simulation viewer", font=(DEFAULT_FONT, DEFAULT_HEADERS_SIZE), bootstyle=(LIGHT, INVERSE)).grid(row=0, columnspan=MAX_COLS)
    box_label, max_prisoners = 1, self.numberOfPrisoners.get()
    print(box_label)
    print(self.numberOfPrisoners.get())
    for row in range(1, MAX_ROWS + 1):
      for col in range(MAX_COLS):
          if (box_label > max_prisoners):
            break
          ttk.Label(self.matrix_frame, image=img, bootstyle=(LIGHT, INVERSE)).grid(row=row, column=col, padx=DEFAULT_PADDING)
          ttk.Label(self.matrix_frame, text=box_label, bootstyle=(SECONDARY, INVERSE)).grid(row=row, column=col, sticky=S)
          box_label += 1
          
          
    self.matrix_frame.image = img
    ttk.Label(self.matrix_frame, text="Average solution time: ", font=(DEFAULT_FONT, DEFAULT_FONT_SIZE), bootstyle=(LIGHT, INVERSE))\
      .grid(row=MAX_ROWS + 1, columnspan=MAX_COLS, pady=DEFAULT_PADDING)
    self.matrix_frame.pack(padx=DEFAULT_PADDING, pady=DEFAULT_PADDING)

what I did to update the matrix

i want to create an update function that destroy the matrix_farame and create him again with the current numbers of objects.

  def _update_matrix(self):   
        for i in self.matrix_frame.winfo_children():
          i.destroy()
        self.root.update_idletasks()
        self._drawBoxMatrix()

0 Answers0