Re: [Tutor] Tkinter layout question

2017-04-30 Thread Alan Gauld via Tutor
On 26/04/17 07:56, Phil wrote:

>> Your messages come into the moderation queue, I'm
> 
> Thanks Alan, maybe the reason ...is because I'm 
> on the bounces list.

I don;t know what bounces list you mean but it looks
like your messages are going through directly now,
I don't know what changed...

Post a test to see. Messages typically arrive within
5-15 minutes of posting if not moderated.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-26 Thread Phil
On Tue, 25 Apr 2017 23:27:05 +0100
Alan Gauld via Tutor  wrote:


> Your messages come into the moderation queue, I'm
> not sure why because the moderation flag is not
> set on your account(it is automatically for new
> members).
> 
> I'll have a closer look next eek when I get back.

Thanks Alan, maybe the reason that I'm in the moderation queue is because I'm 
on the bounces list. I'm on six different bounces lists but I'm still receiving 
e-mail so I suppose there isn't a real problem.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-25 Thread Alan Gauld via Tutor

On 24/04/17 23:40, Phil wrote:


By the way, I notice that my messages to this list,

> and not other's, can take up to four hours

Your messages come into the moderation queue, I'm
not sure why because the moderation flag is not
set on your account(it is automatically for new
members).

I'll have a closer look next eek when I get back.

Alan G


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-24 Thread Phil
On Mon, 24 Apr 2017 20:02:32 +0100
Alan Gauld via Tutor  wrote:

> On 24/04/17 01:50, Phil wrote:
> > On Mon, 24 Apr 2017 09:24:55 +1000
> > Phil  wrote:
> >
> >> On Sun, 23 Apr 2017 09:39:54 +0200
> >> Sibylle Koczian  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

Thank you Alan, problem solved. All this horsing around with adding and 
subtracting 1 had initially led to a mass of confusing code hence the posting 
of my second message on this subject. Don't bother with that message, it's 
complete nonsense.

By the way, I notice that my messages to this list, and not other's, can take 
up to four hours (sometimes longer) to appear, is that normal? I'm on a bounces 
list, is that the reason? Probably not since I'm on several bounces lists.

Anyway, enjoy your holiday.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-24 Thread Alan Gauld via Tutor

On 24/04/17 20:02, Alan Gauld via Tutor wrote:


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



I should point out that to use my table code for your sudoku you need to
- remove the headings
- change the Labels to Entry widgets
- write get/set methods to access the entry data

Alan G

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-24 Thread Alan Gauld via Tutor

On 24/04/17 01:50, Phil wrote:

On Mon, 24 Apr 2017 09:24:55 +1000
Phil  wrote:


On Sun, 23 Apr 2017 09:39:54 +0200
Sibylle Koczian  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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-24 Thread Phil
On Sun, 23 Apr 2017 11:28:51 +0200
Peter Otten <__pete...@web.de> wrote:

> Consider the function make_a_cake(). If you use it
> 
> eat_a_piece_of(make_a_cake())
> eat_a_piece_of(make_a_cake())
> 
> that's short for
> 
> one_cake = make_a_cake()
> eat_a_piece_of(one_cake)
> 
> another_cake = make_a_cake()
> eat_a_piece_of(another_cake)
> 
> i. e. you had two pieces of cake, one piece of each of two cakes.
> 
> If you write
> 
> cake = make_a_cake()
> eat_a_piece_of(cake)
> eat_a_piece_of(cake)
> 
> you have still eaten two pieces of cake but both are taken from the
> same cake.
> 
> Likewise when you write
> 
> root = tk.Tk()
> first_table = DisplayTable(root)
> second_table = DisplayTable(root)
> 
> both tables share the same instance of the Tk class.
>  
> > Also I found that root.mainloop() isn't necessary in that the
> > result is the same with or without. Perhaps it serves some other
> > purpose?
> 
> Try running it from the command line, not in idle. In every tkinter
> program there must be a main loop to respond to events.

Thank you again Peter for taking the time to answer my question.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-23 Thread Phil
On Mon, 24 Apr 2017 09:24:55 +1000
Phil  wrote:

> On Sun, 23 Apr 2017 09:39:54 +0200
> Sibylle Koczian  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?
> 
> Thanks Sibylle, I eventually stumbled upon the answer using my usual
> trial-and-error method. The 3, as in the number of cells, was the key.

Actually, that's not correct either.

Say I want the 7th cell in the first line of a 9 x 9 grid, that would be x = 7, 
y = 1. divmod(7,1) = 2,1 or the first cell in grid 3. So far so good.

Another example, x = 4, y = 3. divmod(4,3) = 1,1. What I need here is grid 2 x 
= 1 and y = 3.

Further complications are, arrays, or lists in Python, start a 0 and divmod may 
not be the answer because divide by 0 is not possible. Making adjustments for 
these two possibilities has resulted in complicated code that does give the 
desired result. Of course, I may have misunderstood the intention of Alan's 
mapping method.

So, what I need is a function to map from a 9 x 9 grid to a cell in a 3 x 3 
grid.

My feeble simplified attempt is as follows:

def map_cell(x,y):
return divmod(x,y)

while(True):
x,y = input().split(" ")
print (map_cell(int(x), int(y)))

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-23 Thread Phil
On Sun, 23 Apr 2017 09:39:54 +0200
Sibylle Koczian  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?

Thanks Sibylle, I eventually stumbled upon the answer using my usual 
trial-and-error method. The 3, as in the number of cells, was the key.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-23 Thread Peter Otten
Phil wrote:

> On Sun, 23 Apr 2017 09:52:16 +0200
> Peter Otten <__pete...@web.de> wrote:
> 
>> If you wrote the above with Buttons instead of DisplayTables you'd
>> encounter the same behaviour. The problem is that you call
>> tkinter.Tk() twice (which is generally a recipe for disaster; if you
>> want multiple windows use tkinter.Toplevel() for all but the first
>> one).
>> 
>> Once you have fixed that you should be OK:
>> 
>> import tkinter as tk
>> import table_class
>> 
>> root = tk.Tk()
>> 
>> tab = table_class.DisplayTable(root,
>> ["Left","middle","Right"],
>> [[1,2,1],
>> [3,4,3],
>> [5,6,5]],
>> datacolor='blue',
>> cellcolor='yellow',
>> gridcolor='red',
>> hdcolor='black')
>> 
>> second_tab = table_class.DisplayTable(root,
>> ["Left","middle","Right"],
>> [[1,2,1],
>> [3,4,3],
>> [5,6,5]],
>> datacolor='blue',
>> cellcolor='green',
>> gridcolor='red',
>> hdcolor='black')
>> 
>> tab.pack(side=tk.LEFT)
>> second_tab.pack()
>> 
>> root.mainloop()
> 
> Thank you again Peter. Of course your changes worked but at the moment I'm
> not sure why.
> 
> if root = tk.Tk() then why isn't table_class.DisplayTable(root, the same
> as table_class.DisplayTable(tk.Tk(),. Obviously it isn't but I don't know
> why.

Consider the function make_a_cake(). If you use it

eat_a_piece_of(make_a_cake())
eat_a_piece_of(make_a_cake())

that's short for

one_cake = make_a_cake()
eat_a_piece_of(one_cake)

another_cake = make_a_cake()
eat_a_piece_of(another_cake)

i. e. you had two pieces of cake, one piece of each of two cakes.

If you write

cake = make_a_cake()
eat_a_piece_of(cake)
eat_a_piece_of(cake)

you have still eaten two pieces of cake but both are taken from the same 
cake.

Likewise when you write

root = tk.Tk()
first_table = DisplayTable(root)
second_table = DisplayTable(root)

both tables share the same instance of the Tk class.
 
> Also I found that root.mainloop() isn't necessary in that the result is
> the same with or without. Perhaps it serves some other purpose?

Try running it from the command line, not in idle. In every tkinter program 
there must be a main loop to respond to events.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-23 Thread Phil
On Sun, 23 Apr 2017 09:52:16 +0200
Peter Otten <__pete...@web.de> wrote:

> If you wrote the above with Buttons instead of DisplayTables you'd
> encounter the same behaviour. The problem is that you call
> tkinter.Tk() twice (which is generally a recipe for disaster; if you
> want multiple windows use tkinter.Toplevel() for all but the first
> one). 
> 
> Once you have fixed that you should be OK:
> 
> import tkinter as tk
> import table_class
> 
> root = tk.Tk()
> 
> tab = table_class.DisplayTable(root,
> ["Left","middle","Right"],
> [[1,2,1],
> [3,4,3],
> [5,6,5]],
> datacolor='blue',
> cellcolor='yellow',
> gridcolor='red',
> hdcolor='black')
> 
> second_tab = table_class.DisplayTable(root,
> ["Left","middle","Right"],
> [[1,2,1],
> [3,4,3],
> [5,6,5]],
> datacolor='blue',
> cellcolor='green',
> gridcolor='red',
> hdcolor='black')
> 
> tab.pack(side=tk.LEFT)
> second_tab.pack()
> 
> root.mainloop()

Thank you again Peter. Of course your changes worked but at the moment I'm not 
sure why.

if root = tk.Tk() then why isn't table_class.DisplayTable(root, the same as 
table_class.DisplayTable(tk.Tk(),. Obviously it isn't but I don't know why.

Also I found that root.mainloop() isn't necessary in that the result is the 
same with or without. Perhaps it serves some other purpose?

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-23 Thread Peter Otten
Phil wrote:

> On Thu, 20 Apr 2017 13:43:07 +0100
> Alan Gauld via Tutor  wrote:
> 
>> If still confused drop a question here.
> 
> I hope I'm not over doing the questions here. I'm only posting after hours
> of experimenting and Internet searching.
> 
> How do I create multiple instances of the table on the one frame? I think
> the table class as presented is not capable of that. If I create multiple
> instances like this then, of course, I end up with two instances of the
> same frame.
> 
> import tkinter as tk
> import table_class
> 
> tab = table_class.DisplayTable(tk.Tk(),
> ["Left","middle","Right"],
> [[1,2,1],
> [3,4,3],
> [5,6,5]],
> datacolor='blue',
> cellcolor='yellow',
> gridcolor='red',
> hdcolor='black')
> 
> 
> second_tab = table_class.DisplayTable(tk.Tk(),
> ["Left","middle","Right"],
> [[1,2,1],
> [3,4,3],
> [5,6,5]],
> datacolor='blue',
> cellcolor='green',
> gridcolor='red',
> hdcolor='black')
> 
> second_tab.pack(side = tk.LEFT)
> tab.pack()
> 
> I've tried different pack options including packing onto the parent frame.
> 

If you wrote the above with Buttons instead of DisplayTables you'd encounter 
the same behaviour. The problem is that you call tkinter.Tk() twice (which 
is generally a recipe for disaster; if you want multiple windows use 
tkinter.Toplevel() for all but the first one). 

Once you have fixed that you should be OK:

import tkinter as tk
import table_class

root = tk.Tk()

tab = table_class.DisplayTable(root,
["Left","middle","Right"],
[[1,2,1],
[3,4,3],
[5,6,5]],
datacolor='blue',
cellcolor='yellow',
gridcolor='red',
hdcolor='black')

second_tab = table_class.DisplayTable(root,
["Left","middle","Right"],
[[1,2,1],
[3,4,3],
[5,6,5]],
datacolor='blue',
cellcolor='green',
gridcolor='red',
hdcolor='black')

tab.pack(side=tk.LEFT)
second_tab.pack()

root.mainloop()




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-23 Thread Sibylle Koczian

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?



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-22 Thread Phil
On Thu, 20 Apr 2017 13:43:07 +0100
Alan Gauld via Tutor  wrote:

> If still confused drop a question here.

I hope I'm not over doing the questions here. I'm only posting after hours of 
experimenting and Internet searching.

How do I create multiple instances of the table on the one frame? I think the 
table class as presented is not capable of that. If I create multiple instances 
like this then, of course, I end up with two instances of the same frame.

import tkinter as tk
import table_class

tab = table_class.DisplayTable(tk.Tk(),
["Left","middle","Right"],
[[1,2,1],
[3,4,3],
[5,6,5]],
datacolor='blue',
cellcolor='yellow',
gridcolor='red',
hdcolor='black')


second_tab = table_class.DisplayTable(tk.Tk(),
["Left","middle","Right"],
[[1,2,1],
[3,4,3],
[5,6,5]],
datacolor='blue',
cellcolor='green',
gridcolor='red',
hdcolor='black')

second_tab.pack(side = tk.LEFT)
tab.pack()

I've tried different pack options including packing onto the parent frame.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-22 Thread Phil
On Sat, 22 Apr 2017 10:37:19 +0200
Peter Otten <__pete...@web.de> wrote:

> That's unnecessary. The code protected by 'if __name__ == "__main__"'
> is not executed when the module is imported. In fact that's the very
> purpose of this idiom.

> 
> "Best practice" is to avoid star imports which bind every name from 
> table_class that does not start with "_", including 'tk' to the same
> name in the importing module.
> 

Thank you Petter for explaining these points, most helpful.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-22 Thread Peter Otten
Phil wrote:

> On Thu, 20 Apr 2017 13:43:07 +0100
> Alan Gauld via Tutor  wrote:
> 
>> If still confused drop a question here.
> 
> Maybe not totally confused, more a question of best practice.
> 
> Using your example table class, I commented out all from, and including,
> "if __name__ == "__main__":" down and saved the file as table_class.py. 

That's unnecessary. The code protected by 'if __name__ == "__main__"' is not 
executed when the module is imported. In fact that's the very purpose of 
this idiom.

> I
> then created test.py as follows:
> 
> from table_class import *

"Best practice" is to avoid star imports which bind every name from 
table_class that does not start with "_", including 'tk' to the same name in 
the importing module.

Instead be explicit:

import tkinter as tk
import table_class

...

tab = table_class.DisplayTable(...)

That way the maintainer of table_class.py only has to be careful to keep 
DisplayTable compatible. A change to the import from

import tkinter as tk

to

import tkinter

or the definition of names that collide with names in other modules leaves 
client code unaffected.

> top = tk.Tk()
> 
> tab = DisplayTable(top,
> ["Left","middle","Right"],
> [[1,2,1],
> [3,4,3],
> [5,6,5]],
> datacolor='blue',
> cellcolor='yellow',
> gridcolor='red',
> hdcolor='black')
> 
> tab.pack()
> 
> Two questions:
> I can see where tk comes from but I'm unsure of the origin of Tk() other
> than a reference to tkinter. Have I used you table class correctly? It
> works, of course, but it doesn't look correct.
> 


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-21 Thread Phil
On Thu, 20 Apr 2017 13:43:07 +0100
Alan Gauld via Tutor  wrote:

> If still confused drop a question here.

Maybe not totally confused, more a question of best practice.

Using your example table class, I commented out all from, and including, "if 
__name__ == "__main__":" down and saved the file as table_class.py. I then 
created test.py as follows:

from table_class import *

top = tk.Tk()

tab = DisplayTable(top,
["Left","middle","Right"],
[[1,2,1],
[3,4,3],
[5,6,5]],
datacolor='blue',
cellcolor='yellow',
gridcolor='red',
hdcolor='black')

tab.pack()

Two questions:
I can see where tk comes from but I'm unsure of the origin of Tk() other than a 
reference to tkinter.
Have I used you table class correctly? It works, of course, but it doesn't look 
correct.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-20 Thread Alan Gauld via Tutor
On 20/04/17 13:43, Alan Gauld via Tutor wrote:

> So the 7th element becomes
> divmod(7) -> 2,1
> 
> ie. element 7 maps to the 2nd cell, element 1

That should of course be the 3rd cell (0 based), oops.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-20 Thread Alan Gauld via Tutor
On 20/04/17 10:33, Phil wrote:

>> So, for a Suduko grid put 3x3 Entry boxes into a Frame.
>> Then put 3x3 such frames into another frame.
> 
> OK, so I'll go back to my original idea and use edit boxes. A grid of 9 x 9 
> edit boxes does actually work and it makes it easy to keep track of the 
> digits. The first digit is [0,0], the first digit on the second line is [1,0] 
> etc. 

Nine 3 x 3 boxes could add some complication to digit tracking.

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

ie. element 7 maps to the 2nd cell, element 1
You can create a simple helper function that takes an x,y pair from
the 9x9 view and returns two pairs identifying the cell coordinates.

And having the smaller 3x3 cells works when checking that each 3x3
cell has the 9 unique numbers too.

> I did actually get my canvas version to the point where I could 
> enter digits into the cells but I had to enter them in sequence

Yes, that's exactly the kind of problems you hit and its a
terrible user experience. Far better to use the facilities
the GUI gives you for free. For example tab will move the
cursor from cell to cell etc.

> Thank you for the table example. I'm not sure what "tab = DisplayTable" 

It creates an instance of the table.

tab = DisplayTable(top,   # the containing widget/frame
  ["Left","Right"],   # table headings
  [[1,2], # The table data, 3x2 items
   [3,4],
   [5,6]],
  datacolor='green')  # the color used to draw the data

Note:
the constructor allows other colours to be specified too
such as the headings the grid lines and the cell background.
You may want to hard code those in a simplified version of
my class.


Hopefully when you run it you'll understand, especially
if you tweak the example instance options. For example
here is a full featured version:

tab = DisplayTable(top,
   ["Left","middle","Right"],
   [[1,2,1],
[3,4,3],
[5,6,5]],
   datacolor='blue',
   cellcolor='yellow',
   gridcolor='red',
   hdcolor='black')


If still confused drop a question here.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-20 Thread Phil
On Thu, 20 Apr 2017 09:27:27 +0100
Alan Gauld via Tutor  wrote:

> Eek! that's a recipe for premature baldness!

Baldness is not a problem, however, slowing the onset of dementia is the aim of 
this project.

> So, for a Suduko grid put 3x3 Entry boxes into a Frame.
> Then put 3x3 such frames into another frame.

OK, so I'll go back to my original idea and use edit boxes. A grid of 9 x 9 
edit boxes does actually work and it makes it easy to keep track of the digits. 
The first digit is [0,0], the first digit on the second line is [1,0] etc. Nine 
3 x 3 boxes could add some complication to digit tracking.

> Don't try to reinvent all of that yourself, it will
> result in tears. (Think about how you will control
> cursor movement, deletions, selections etc etc)

I did actually get my canvas version to the point where I could enter digits 
into the cells but I had to enter them in sequence so that the logic part of 
the program knew where the digits were. It was all becoming somewhat 
complicated.

Thank you for the table example. I'm not sure what "tab = DisplayTable" does at 
the moment, I'll have to run it to find out.

Thank you for your detailed answer, more food for though.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-20 Thread Alan Gauld via Tutor
On 19/04/17 23:48, Phil wrote:
> I created a 9 x 9 grid on a canvas which looks much better.

> I can display digits in the centre of the squares but 
> entering the digits from the keyboard seems to be beyond me.

Eek! that's a recipe for premature baldness!
Canvas is designed to display things not for user input.
Trying to read keypresses and the like is going to be very
hard to get right. Use the widgets that are designed for
that, namely Entry boxes.

As to layout, use Frames. Lots of frames.
Frames are the key to layout in most GUIs and especialy
so in Tkinter.

So, for a Suduko grid put 3x3 Entry boxes into a Frame.
Then put 3x3 such frames into another frame. Personally
I'd create a class to represent the 3x3 Entry frame
and then create 9 instances of these.

As to the spacing between widgets (the frames in
this case) use the various padx/pady and fill options.
You can have incredibly fine grained control over
layout using the tools that Tkinter gives you. If
you combine that with the different layout managers
(pack, grid,place) - which can be mixed and matched
as needed using more Frames - you have hugely
powerful control over layout.

Don't try to reinvent all of that yourself, it will
result in tears. (Think about how you will control
cursor movement, deletions, selections etc etc)

BTW I posted a simple display-only grid here a
few weeks ago. It might prove helpful as a basis
for the 3x3 cells so, here it is again:

try:
  import Tkinter as tk  # v2
except ImportError:
  import tkinter as tk  # v3

class DisplayTable(tk.Frame):
def __init__(self, parent, headings, data,
 hdcolor='red', datacolor='black',
 gridcolor= 'black', cellcolor='white'):
tk.Frame.__init__(self, parent, bg=gridcolor)

if len(headings) != len(data[0]): raise ValueError
self.headings = headings

for index,head in enumerate(headings):
width = len(str(head))
cell = tk.Label(self,text=str(head),
bg=cellcolor, fg=hdcolor, width=width)
cell.grid(row=0,column=index, padx=1, pady=1)

for index,row in enumerate(data):
self.addRow(index+1,row,datacolor,cellcolor)

def addRow(self, row, data, fg='black', bg='white'):
for index, item in enumerate(data):
width = len(str(self.headings[index]))
cell = tk.Label(self,text=str(item),
fg=fg, bg=bg, width=width)
cell.grid(row=row, column=index, padx=1,pady=1)


if __name__ == "__main__":
top = tk.Tk()
tab = DisplayTable(top,
   ["Left","Right"],
   [[1,2],
[3,4],
[5,6]],
   datacolor='green')
tab.pack()
top.mainloop()



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-20 Thread Palm Tree
for entering digits on the canva i think better create a sort of sudoku
generator and display it on the canva in a create_text object.

On 20 Apr 2017 05:24, "Phil"  wrote:

> I'm looking for ideas here.
>
> A working solution for my sudoku solver is a 9 x 9 grid of entry boxes but
> it looks a bit basic. So I created a 9 x 9 grid on a canvas which looks
> much better. I can display digits in the centre of the squares but entering
> the digits from the keyboard seems to be beyond me. I experimented with
> entering a digit at the mouse location but it all seems to be too
> complicated. Perhaps someone can offer a repetitively simple solution?
>
> A second experiment involved the earlier grid of entry boxes but with a
> space between every third row and column. This seems to be more achievable,
> eventually.
>
> Something along these lines:
>
> for i in range(9):
> if i % 4 == 0:
> place a blank text label
> else:
> place an entry box
>
> So, how might I enter digits into a grid on a canvas or how could I create
> a space between a grid entry boxes?
>
> --
> Regards,
> Phil
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question - solved

2017-04-20 Thread Phil
On Wed, 19 Apr 2017 22:21:28 -0500

Just to save people answering this question unnecessarily I have solved the 
immediate problem. I can now enter a digit at the mouse coordinates. Some 
refinement is still necessary.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-20 Thread Phil
On Wed, 19 Apr 2017 22:21:28 -0500
boB Stepp  wrote:

Thank you Bob and Palm for your replies. They have given me something more tto 
think about.
ideas
> I don't know (now) how to solve your challenges below.  But if I were
> trying to figure this out, I would try to find more complex tkinter
> example applications

I have searched the Internet for hours looking for a game example. Most 
examples demonstrate the use of specific widgets including the canvas and 
frames but not in a class context. Putting the pieces together is difficult but 
I will persevere.

A reference book full of examples is the obvious way to go I suppose. However, 
it is difficult me to manage paper books because the space they take up and 
their weight. I do have one general Python e-book but following the few 
examples is tedious to the nth degree because the text of the examples is so 
small that I need a magnifying glass to read them.

Anyway, maybe I can justify one book, I'll give it some thought. The massive 
tome by Lutz comes to mind.

I'll give your keyboard entry suggestion some more thought.

-- 
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter layout question

2017-04-19 Thread boB Stepp
I don't know (now) how to solve your challenges below.  But if I were
trying to figure this out, I would try to find more complex tkinter
example applications that contain some of the issues I'd be interested
in.  Then I would dig into the code, find the relevant pieces, and
then start playing with them until I understood them.  I know Alan
suggested some resources in one of your earlier threads.  One of the
books he suggested was, "Python and Tkinter Programming" by John E.
Grayson.  It is a fairly old book from the Python 2 days, copyrighted
2000.  However, I own this book and have gotten a lot of use out of
it.  He has _lots_ of complete examples, including such things as a
scientific calculator that looks like something HP sells!  The other
book, "Programming Python" by Mark Lutz has a very large section on
tkinter with plenty of examples.  I have gotten much good use out this
book as well.  It is much more recent, covering Python 3 (and mentions
Python 2 differences as well).  And then there is the Internet.
Surely you could find relevant material out there.  Maybe even the
source code for IDLE would be helpful.  I understand it is written
using tkinter.

On Wed, Apr 19, 2017 at 5:48 PM, Phil  wrote:
> I'm looking for ideas here.
>
> A working solution for my sudoku solver is a 9 x 9 grid of entry boxes but it 
> looks a bit basic. So I created a 9 x 9 grid on a canvas which looks much 
> better. I can display digits in the centre of the squares but entering the 
> digits from the keyboard seems to be beyond me. I experimented with entering 
> a digit at the mouse location but it all seems to be too complicated. Perhaps 
> someone can offer a repetitively simple solution?
>

Surely doing the keyboard entries isn't a horribly difficult problem?
It sounds like it breaks down to two different types of key bindings:
(1) Binding some keys (perhaps the arrow keys) to change focus from
cell to cell; and, (2) binding keys to do the actual numerical data
entry in each cell.

> A second experiment involved the earlier grid of entry boxes but with a space 
> between every third row and column. This seems to be more achievable, 
> eventually.
>

I think I have done some of this type of "spacing" stuff using empty
narrow frames.  I don't know if would be helpful or not for you in
your use case, but perhaps it is something to think about.  In the
Grayson book he has lots of examples of achieving very precise spacing
and placement of widgets, etc.

Just some thoughts.  Perhaps they might inspire a creative thought or
two.  Of course one of the experienced tkinter pros out there might
chime in with very detailed, specific help.


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor