I have been fighting the same bug for weeks now with zero success: I am trying to get images to come up on my buttons, but they utterly refuse to show up. Regardless of whether I used my old Python 2.5.1 or now 2.6.5, the following code:
'''Minesweeper.'''
from Tkinter import *
import Tkinter as tk
import random
BEGINNER = 1
INTERMEDIATE = 2
EXPERT = 3
OFFSET = 2
BUTTON_SIZE = 2
class Board(Frame, object):
def __init__(self, master, difficulty):
Frame.__init__(self, master)
if difficulty == BEGINNER:
self.x = 9
self.y = 9
self.mines = 10
elif difficulty == INTERMEDIATE:
self.x = 16
self.y = 16
self.mines = 40
elif difficulty == EXPERT:
self.x = 30
self.y = 16
self.mines = 99
self.grid()
self.create_widgets()
def create_widgets(self):
#Create the grid.
self.square = []
self.isAMine = []
self.selected = []
for i in range(self.x):
squareColumn = []
mineColumn = []
selectedColumn = []
for j in range(self.y):
squareColumn.append(Button(self, width = 3, height = 1))
squareColumn[j].grid(row = j + OFFSET, column = i)
mineColumn.append(False)
selectedColumn.append(False)
self.square.append(squareColumn)
self.isAMine.append(mineColumn)
self.selected.append(selectedColumn)
#Plant the mines.
print 'Dimensions:', self.x, self.y
for i in range(self.mines):
mineSquare = random.randrange(self.x * self.y)
mine_y = mineSquare / self.x
mine_x = mineSquare % self.x
self.isAMine[mine_x][mine_y] = True
self.square[mine_x][mine_y]['text'] = 'X' #temp line; shows mines
#photo = tk.PhotoImage(file="1.gif")
#self.square[mine_x][mine_y]['image'] = photo #temp line; shows mines
for i in range(self.y):
for j in range(self.x):
self.square[j][i]['command'] = lambda x=j, y=i: self.hit(x, y)
#Runs when a button (square) is clicked.
def hit(self, x, y):
self.selected[x][y] = True
self.square[x][y].config(relief=SUNKEN)
#print x, y
if self.isAMine[x][y]:
print 'Mine found. Location:', x, y
else:
#Look at all eight neighbors and see if they are mines.
#x>0, etc. avoid looking off the edge of the map.
adjMines = 0
if (x > 0 and y > 0) and self.isAMine[x-1][y-1]: #NW
adjMines+=1
if y > 0 and self.isAMine[x][y-1]: #N
adjMines+=1
if (x < self.x-1 and y > 0) and self.isAMine[x+1][y-1]: #NE
adjMines+=1
if x > 0 and self.isAMine[x-1][y]: #W
adjMines+=1
if x < self.x-1 and self.isAMine[x+1][y]: #E
adjMines+=1
if (x > 0 and y < self.y-1) and self.isAMine[x-1][y+1]: #SW
adjMines+=1
if y < self.y-1 and self.isAMine[x][y+1]: #S
adjMines+=1
if (x < self.x-1 and y < self.y-1) and\
self.isAMine[x+1][y+1]: #SE
adjMines+=1
if adjMines>0:
self.square[x][y]['text'] = adjMines
try:
image = []
image.append(PhotoImage(file='1.gif').zoom(2))
except (IOError):
print 'File missing.'
else:
if adjMines != 0 and adjMines < 2:
self.square[x][y]['text'] = ''
self.square[x][y]['image'] = image[adjMines-1]
else: #adjMines == 0
#If none of the adjacent squares have mines, it is safe to hit
#them all. Just like the official game, this game does
#precisely that.
if (x > 0 and y > 0) and not self.selected[x-1][y-1]:
Board.hit(self, x-1, y-1) #NW
if y > 0 and not self.selected[x][y-1]: #N
Board.hit(self, x, y-1)
if (x < self.x-1 and y > 0) and not self.selected[x+1][y-1]: #NE
Board.hit(self, x+1, y-1)
if x > 0 and not self.selected[x-1][y]: #W
Board.hit(self, x-1, y)
if x < self.x-1 and not self.selected[x+1][y]: #E
Board.hit(self, x+1, y)
if (x > 0 and y < self.y-1) and not self.selected[x-1][y+1]: #SW
Board.hit(self, x-1, y+1)
if y < self.y-1 and not self.selected[x][y+1]: #S
Board.hit(self, x, y+1)
if (x < self.x-1 and y < self.y-1) and\
not self.selected[x+1][y+1]:
Board.hit(self, x+1, y+1) #SE
self.square[x][y]['command'] = ''
def main():
root = Tk()
root.title('Minesweeper')
root.geometry('450x300')
diff = int(raw_input( 'Select your difficulty level: '))
theBoard = Board(root, diff)
root.mainloop()
main()
Produces the following results (the button size is actually different than in this photo, but the image results--the heart of this problem--are exactly the same):
http://img717.yfrog.com/img717/8660/minegoofup.jpg
I am completely at wit's end. I have tried using PIL, I have tried resizing the buttons, and zooming the images, and saving the images as .gif's and .jpeg's and .bmp's and you name it. Absolutely nothing is working. I have a major assignment due within days and absolutely MUST figure this out. Someone please show me what the heck I am doing wrong and what I can do to fix it.