Reputation: 159
So I'm creating the game of life in python, and would like to create a grid on top of a canvas using the tkinter class of python. Thanks in advance.
To make the grid I'm using the create_line() function of the canvas class and looping through using the range() function so that my lines are drawn to the width and the height of the canvas, using two separate for loops one for width and one for the height. The loops I have gotten are taken from the following piece of code on stackoverflow: How to create a grid on tkinter in python? I thought I understood what is happening but what I thought should happen has not
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
canvas = Canvas(window, background='white', width=800, height=600)
def create_grid(canvas):
width = canvas.winfo_width() # gets width of the canvas
height = canvas.winfo_height() # gets height of the canvas
for line in range(0, width, 1): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 1):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
create_grid(canvas)
canvas.grid(row=0, column=0)
window.mainloop()
My expected results are to have a white canvas with vertical and horizontal lines stretching the width and height of the canvas. But my actual results are just a white canvas with no lines on top.
Upvotes: 0
Views: 4036
Reputation: 76184
I don't trust winfo_width
and winfo_height
to give accurate values in this context. According to https://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_height-method, the functions simply return 1 if you haven't packed/gridded/placed the widget yet, and even if you have, it might still return 1 if the window's event loop hasn't updated lately. You already know that you want the width to be 800 and the height to be 600, so you may as well define them as such directly.
Another problem is that your range
calls have a step
argument of 1. This means that the lines will be one pixel apart, which will effectively paint the entire canvas black. I recommend a larger step.
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
def create_grid(window):
width = 800
height = 600
canvas = Canvas(window, background='white', width=width, height=height)
for line in range(0, width, 10): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 10):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
canvas.grid(row=0, column=0)
create_grid(window)
window.mainloop()
Result:
Upvotes: 3