Question:Programming a game grid ...

David dwblas at gmail.com
Wed Jun 27 19:24:30 EDT 2012


First, you should be getting an error on
vars()[var] = Button(f3, text = "00", bg = "white")
as vars() has not been declared and it does not appear to be valid Python syntax.  I don't see a reason to store a reference to the button since you won't be modifying them.  Also, you can not mix pack() and grid().  It produces unpredictable results.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

def leftclick(*args):
    print "leftclick called"

def rightclick(*args):
    print "rightclick called"

root = tk.Tk() 

f3 = tk.Frame(root, bg = "white", width = 500) 
f3.grid()

this_row=0
this_column=0
for ctr in range(0, 89):
    b = tk.Button(f3, text = "%0d" % (ctr), bg = "white") 
    b.grid(row=this_row, column=this_column) 
    b.bind('<Button-1>', leftclick)   # bind left mouse click 
    b.bind('<Button-3>', rightclick)   # bind left mouse click
    this_column += 1
    if this_column > 6:
        this_column=0
        this_row += 1
root.title('Puzzle Grid') 
root.mainloop()



More information about the Python-list mailing list