Hi,
Not sure this is the place to submit such a query, please re-direct me
to the correct place.
I implemented a simple Lights Out game which works with the old Python
2.6 (and 2.7 in Windows).
After upgrading to SUSE 13.1 which comes with Python 2.7.5 I get the
following error:
*Exception in Tkinter callback**
**Traceback (most recent call last):**
** File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1470, in __call__**
** return self.func(*args)**
** File "./LightsOut.py", line 28, in mouse**
** grid_info = event.widget.grid_info()**
** File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1978, in grid_info**
** self.tk.call('grid', 'info', self._w))**
**TypeError: coercing to Unicode: need string or buffer,
_tkinter.Tcl_Obj found**
*
I get the same error with Python 3.3.2 (which also comes with SUSE
13.1). I attached the source code for the game, please run with
"./LightsOut 3".
Any help would be greatly appreciated.
Thanks,
Horace
#!/usr/bin/python
import sys
import Tkinter as tk
import tkFont
# The ones below are for Python 3
#import tkinter as tk
#import tkinter.font as tkFont
class Application(tk.Frame):
def __init__(self, N, master=None):
tk.Frame.__init__(self, master)
self.N = int(N)
self.x_pos = 0
self.y_pos = 0
self.grid_dict = []
self.color_dict = []
self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
self.helv36 = tkFont.Font(family='Helvetica', size=36, weight='bold', slant="italic", underline=1, overstrike=0)
self.createWidgets()
def setColor(self, arg):
if arg:
return("green")
else:
return("blue")
def mouse(self, event):
grid_info = event.widget.grid_info()
#print "row:", grid_info["row"], "column:", grid_info["column"]
self.x_pos = int(grid_info["column"])
self.y_pos = int(grid_info["row"])
for i in [-1, 0, 1]:
if 0 <= self.x_pos + i and self.x_pos + i < self.N:
self.color_dict[self.y_pos][self.x_pos + i] = not self.color_dict[self.y_pos][self.x_pos + i] #Reverse color when clicked.
for i in [-1, 1]:
if 0 <= self.y_pos + i and self.y_pos + i < self.N:
self.color_dict[self.y_pos + i][self.x_pos] = not self.color_dict[self.y_pos + i][self.x_pos] #Reverse color when clicked.
def changeColor(self):
#print("self.x_pos = %s, self.y_pos = %s" % (self.x_pos, self.y_pos))
for i in [-1, 0, 1]:
if 0 <= self.x_pos + i and self.x_pos + i < self.N:
self.grid_dict[self.y_pos][self.x_pos + i].configure(bg = self.setColor(self.color_dict[self.y_pos][self.x_pos + i]),
fg = self.setColor(self.color_dict[self.y_pos][self.x_pos + i]))
for i in [-1, 1]:
if 0 <= self.y_pos + i and self.y_pos + i < self.N:
self.grid_dict[self.y_pos + i][self.x_pos].configure(bg = self.setColor(self.color_dict[self.y_pos + i][self.x_pos]),
fg = self.setColor(self.color_dict[self.y_pos + i][self.x_pos]))
def createWidgets(self):
top = self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
for i in range(self.N):
self.grid_dict.append([])
self.color_dict.append([])
for j in range(self.N):
self.color_dict[i].append(False)
self.grid_dict[i].append(tk.Button(self, text=' ', command=lambda: self.changeColor() , font=self.helv36, bg=self.setColor(self.color_dict[i][j]), fg=self.setColor(self.color_dict[i][j])))
self.grid_dict[i][j].grid(row=i, column=j, sticky=tk.N+tk.S+tk.E+tk.W)
self.grid_dict[i][j].bind("<Button-1>", self.mouse)
def main():
N = sys.argv[1]
app = Application(N)
app.master.title('Lights Out')
app.mainloop()
if __name__ == "__main__":
main()
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss