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

Reply via email to