Question

I am new to Tkinter and have developed only one GUI before this. I decided to create a random terrain generator using labels to represent a block, in a 10 x 10 grid. I have finished it and fixed a memory leak with making a large amount of labels. I also implemented a randomizing button and scrolling. However, after approximately 100 (about 98 last I checked) updates of the grid, it will bug out with a grey frame appearing outside of the window and the top part of the world will appear grey, then the program will stop responding. There is no error message, so I have no idea how to fix this. The code is on pastebin at: http://pastebin.com/SASFuQCZ, although it is very long. Any help with fixing this would be greatly appreciated. Thanks.

Was it helpful?

Solution

Maybe someone else can figure it out, but for me your code is simply too difficult to understand. However, even though I can't solve the problem, I can give some advice that might help you cut out perhaps one thousand lines of code, which might then make the problem a little easier to spot.

Are you aware that you can create widgets in loops? You could easily remove several hundred lines of code by using a very simple technique. Also, are you aware that you can change the color of an existing label without destroying it and recreating it?

Finally, you don't need all of those instances of StringVar since you are only storing a color in them. A normal variable will do. StringVars are really only useful when you are trying to tie together the label or value of two or more widgets together. If you want to store cell colors in variables, store them in the same data structure you use for the labels, and then your redraw function merely needs to do one loop to apply each color to each cell.

For example, to set all of the initial colors to "royal blue" you might do something like the following. I use a dictionary, but you could use a list of lists just about as easily:

color = {}
for row in range(10):
    for column in range(10):
        color[(row, column)] = "royal blue"

Next, you can create a grid of 10x10 labels with a the same simple loop. This can replace the 200 lines of code (plus another 100 blank lines) you use to create the original labels with only 6 lines of code.

# to create the labels and store them in a dictionary:
cell = {}
for row in range(10):
    for column in range(10): 
        l = Label(app, text="        ", background=color[(row, column)])
        l.grid(row=row, column=column)
        cell[(row,column)] = l

You can update each label using configure. So, for example, to change label 0,0 to red you would do this:

cell[(0,0)].configure(background="red")

You can retrieve the background color of a label with the cget method. For example, this will print "red":

print cell[(0,0)].cget("background")

Using all of these techniques might cut your code down to just a couple hundred lines, which will make any errors you have easier to spot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top