0

I took this code from the first example here http://zetcode.com/tkinter/drawing/ I refit it so that it would print a map in the same file. There are no inherent errors, and it goes through the loops and even hits all the if statements properly. But in the end the canvas/frame has nothing in it. Can anyone tell me why?

from tkinter import Tk, Canvas, Frame, BOTH, NW


    class Example(Frame):

        def __init__(self, parent):
            Frame.__init__(self, parent)   

            self.parent = parent        
            self.initUI()

        def initUI(self):

            self.parent.title("Board")        
            self.pack(fill=BOTH, expand=1)

            canvas = Canvas(self)

            #The first four parameters are the x,y coordinates of the two bounding points.
            #The top-left and the bottom-right. 
            color = ""
            for x in range(10):
                for y in range(10):
                    if type(landMass[x][y]) is Land:
                        color = "grey" 
                    if type(landMass[x][y]) is Food:
                        color = "green"
                    if type(landMass[x][y]) is Water:
                        color = "blue"
                    if type(landMass[x][y]) is Shelter:
                        color = "black"
                    rec = canvas.create_rectangle(3 + 50 * y, 3 + 50 * x, 53 + 50 * y, 53 + 50 * x , fill=color)
                    text = canvas.create_text(3 + 50 * y, 3 + 50 * x, anchor=NW, fill="white", text=landMass[x][y].elevation)

    def main():

        root = Tk()
        ex = Example(root)
        root.geometry("500x500+500+500")
        root.mainloop()  


    if __name__ == '__main__':
        main() 

1 Answer 1

3

Check the link, you have omitted the call to canvas.pack(fill=BOTH, expand=1) at the end of the function.

After here:

for x in range(10):
    for y in range(10):
        if type(landMass[x][y]) is Land:
            color = "grey" 
        if type(landMass[x][y]) is Food:
            color = "green"
        if type(landMass[x][y]) is Water:
            color = "blue"
        if type(landMass[x][y]) is Shelter:
            color = "black"
        rec = canvas.create_rectangle(3 + 50 * y, 3 + 50 * x, 53 + 50 * y, 53 + 50 * x , fill=color)
        text = canvas.create_text(3 + 50 * y, 3 + 50 * x, anchor=NW, fill="white", text=landMass[x][y].elevation)

You should have:

canvas.pack(fill=BOTH, expand=1)
4
  • Unfortunatly I had already tried that, and the link has the pack in the same place. Apr 14, 2013 at 20:50
  • @EasilyBaffled Well you are wrong! I've tried it myself on your code and it works, it displays everything now after doing that. If you aren't packing the canvas obviously it's not going to show it
    – jamylak
    Apr 14, 2013 at 20:56
  • @EasilyBaffled The link has the pack in the right place but yours doesn't have it at all!
    – jamylak
    Apr 14, 2013 at 20:58
  • 1
    Ah you're right I see what the problem was, I miss read the self.pack(fill=BOTH, expand=1) as canvas.pack(fill=BOTH, expand=1), and that messed with everything Apr 14, 2013 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.