0

Setting

I'm writing a program, using Python's Tkinter, which gives the user a visual representation of a warehouse full of agricultural produce.

The warehouse consists of stacks of boxes, arranged in a grid pattern. In order to avoid confusion with the data structure, I refer to a stack, in this context, as a spot, as in a spot on the warehouse floor.

I've arranged my GUI so that the grid of spots is displayed, and when the user clicks on each spot, a representation of the boxes therein is displayed.

The Problem

The GUI tends to freeze - about one time in twenty - when a spot on the grid is clicked. As far as I can tell, the line in the code causing the freezing is when .grid() is called.

Havint looked at a few similar questions on this site, I have tried using threading, but this hasn't helped.

The Code

This is one of the higher-level classes from the full code, which runs to a few hundred lines.

I'm happy to post more code if that's helpful.

class CS_Viewer:
  def __init__(self, parent):
    self.parent = parent
    self.parent_window = self.parent.get_top()
    self.code = self.parent.coldstore.code
    self.parent.coldstore.reconstruct()
    self.max_max_layers = self.parent.coldstore.get_max_max_layers()
    self.layer = 0
    self.top = Frame(self.parent_window)
    self.top_title = Label(self.top, text="CSEye",
                           font=(main_font, title_pt, "bold"))
    self.subtitle = Label(self.top, text="code="+self.code,
                          font=(main_font, subtitle_pt, "bold"))
    self.main_container = Frame(self.top)
    self.spot_grid = Spot_Grid(self, self.main_container)
    self.box_details = Text(self.main_container,
                            height=details_height, width=details_width,
                            state=DISABLED)
    self.spot_stack = Spot_Stack(self.main_container, None,
                                 self.box_details)
    self.add_headings()
    self.arrange()

  # Add headings to the holster widgets.
  def add_headings(self):
    spot_grid_label = Label(self.main_container, text="Coldstore",
                            font=(main_font, big_pt, "bold"))
    spot_stack_label = Label(self.main_container, text="Spot",
                             font=(main_font, big_pt, "bold"),
                             width=spot_stack_width)
    box_details_label = Label(self.main_container, text="Box",
                              font=(main_font, big_pt, "bold"))
    spot_grid_label.grid(column=0, row=0)
    spot_stack_label.grid(column=1, row=0)
    box_details_label.grid(column=2, row=0)

  # Ronseal.
  def place_spot_stack(self):
    self.spot_stack.get_top().grid(column=1, row=1,
                                   padx=standard_pad, pady=standard_pad)
    # ^^^ The problem seems to be here. ^^^

  # Arrange the object's elements.
  def arrange(self):
    self.top_title.pack()
    self.subtitle.pack()
    self.spot_grid.get_top().grid(column=0, row=1, sticky=N,
                                  padx=standard_pad, pady=standard_pad,
                                  ipadx=inner_pad, ipady=inner_pad)
    self.place_spot_stack()
    self.box_details.grid(column=2, row=1, sticky=N,
                          padx=standard_pad, pady=standard_pad)
    self.main_container.pack()

  # Replace the spot stack widget.
  def replace_spot_stack(self):
    self.spot_stack.get_top().grid_forget()
    Thread(target=self.place_spot_stack).start()

  # Ronseal.
  def get_top(self):
    return self.top
Tom Hosker
  • 526
  • 2
  • 17
  • ***"freezing is when .grid() is called."***: You have to obey, **NOT** to call any `tkinter` GUI method from within your `Thread`. Read [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Feb 10 '20 at 19:15
  • One cause of a tkinter program freezing is when you use both `pack` and `grid` with widgets that share the same container. You might want to do a careful audit of where you're using `pack` and `grid` to make sure you aren't using them incorrectly. Though, as @stovfl mentioned, calling a tkinter method from any thread other than the main thread is a potential cause of unpredictable behavior. – Bryan Oakley Feb 10 '20 at 19:32

0 Answers0