[Tutor] Tkinter layout question
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Apr 24 15:02:32 EDT 2017
On 24/04/17 01:50, Phil wrote:
> On Mon, 24 Apr 2017 09:24:55 +1000
> Phil <phil_lor at bigpond.com> wrote:
>
>> On Sun, 23 Apr 2017 09:39:54 +0200
>> Sibylle Koczian <nulla.epistola at web.de> wrote:
>>
>>> Am 20.04.2017 um 14:43 schrieb Alan Gauld via Tutor:
>>>> Its not too bad you can map the large 9x9 table to the smaller
>>>> units using divmod()
>>>>
>>>> So the 7th element becomes
>>>> divmod(7) -> 2,1
>>>>
>>>
>>> Should be divmod(7, 3), shouldn't it?
Yes, of course, sorry about that!
The 3 of course is the number of items per cell (3x3)
> Say I want the 7th cell in the first line of a 9 x 9 grid,
> that would be x = 7, y = 1.
But you want it mapped to a cell/item pair...
divmod(7,3) -> 2,1
The 3rd cell, 2nd item which is wrong for the item part.
So you need to use:
2, 1-1
Taking item 4 in your 9x9,
divmod(4,3) -> 1,1 cell 1 item 1 so again you need
to subtract 1
So we can write
def map_dimension_to_cell(index):
cell,item = divmod(index,3)
return cell,item-1
And luckily for us that works even on exact boundaries
because cell[-1] is the last item in the cell!
Mapping columns is exactly the same.
Now the only problem is to map your notional 9x9 table 9
indexing from 1 to a Python 9x9 indexing from zero.
The easiest way is to add 1 to the input index:
def map_index_to_cell(index):
cell,item = divmod(index+1,3)
return cell,item-1
You can now pass in the x or y index from your
python 9x9 table to get the cell/item indexes
in your GUI like so:
cell_x,x = map_index_to_cell(x_index)
cell_y,y = map_index_to_cell(y_index)
cell = sudoku_grid[cell_x,cell_y]
item = cell[x,y]
All untested but I think that should work...
And you could wrap that up as a pair of get/set
functions if you so wished.
def get_sudoku_grid(x,y):
# code above
return item
def set_sudoku_grid(x,y,value):
#code above
item = value
Sorry for the late response, I'm on a vacation so
not checking mail that often...
HTH
Alan G
More information about the Tutor
mailing list