0

I'm trying to update all the color boxes in this grid when I click a button to go to new colors, but the button doesn't appear to be working. It should call the displayGrid() function whenever the button is pressed, but nothing seems to be happening? Any help would be appreciated.

import tkinter as tk
from tkinter import *
import random

window = tk.Tk()

containerFrame = LabelFrame(window, text = "Color Picker Grid", highlightthickness = 5)

def displayGrid(): 

    for x in range(5):
        containerFrame.columnconfigure(x, weight=1, minsize=75)
        containerFrame.rowconfigure   (x, weight=1, minsize=75)

        for y in range(5):
            gridFrame = tk.Frame(
                master = containerFrame,
                relief = tk.RAISED,
                borderwidth = 3
            )

            gridFrame.grid(row = x, column = y, padx = 5, pady = 5)  # Sets position of the color square
            gridColor = "#" + ("%06x" % random.randint(0, 0xFFFFFF)) # Creates the randomized color
            gridLabel = tk.Label(                                    # Creates the square itself with the color
                master = gridFrame, 
                text = gridColor,
                font = ('arial bold', '12'),
                fg = "white",
                anchor = "sw",
                height = 10,
                width = 20,
                bg = gridColor
            )

            gridLabel.pack(padx = 5, pady = 5)
            
    containerFrame.pack(fill = "both", expand = "yes")

displayGrid()

refreshButton = Button(window, text = "Refresh", command = displayGrid())
refreshButton.pack(padx = 5, pady = 5)

# To-do, put a black background behind the white text, figure out how to refresh the page for colors and all that

window.mainloop()
  • 2
    You called the function *when you created the Button* (that's what `displayGrid()` means), and used its return value (which is `None`) as the action to take when the Button is clicked. You just want a function name there, without parentheses. Note that your program still wouldn't work quite right even with this change, as you're doing nothing to get rid of the previous set of color boxes - you're just layering the new boxes on top of them. The program will get slower and slower, and take more memory, every time you click the button. – jasonharper Jul 12 '21 at 21:58
  • Instead of creating a new set of widgets each time the button is clicked, you can change the values of one or more the options of the existing ones by calling their [`config()`](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/universal.html) method and specifying them. – martineau Jul 12 '21 at 22:24
  • @jasonharper I see, yeah that works. How would I delete the old grid then? – CircuitBoard Jul 12 '21 at 23:15
  • @martineau how would I do that? Especially for all objects in the grid – CircuitBoard Jul 13 '21 at 18:52
  • You would need to store the widgets you want to update in a container of some sort to make it possible to update them later (using `config()`). In this case I would suggest using a list-of-lists since you're displaying a 2D grid of `Frame`s and `Label`s. In other words a list with 5 "rows" each of which is another list of 5 elements (25 elements in all). Each element could be a single widget or a tuple of two them if you need to be able to update both the `Frame` and `Label` at each position for some reason. – martineau Jul 13 '21 at 19:53

0 Answers0