1

I just debugged most of my MineSweeper Code in Python, but there is an issue with the game functions that determine a win or loss. If I win, it does not show the desired message box and confirm a win. If I lose, it will show an error message that says messagebox (a tkinter built in function) is undefined.

This is my code for the game:

from tkinter import *
import random

...

def CheckWin(self):
    '''Checks if player won'''
    doneList = []
    for key in self.cells.keys():
        if self.cells[key].clicked == True and self.cells[key].value != 9:
            doneList.append(self.cells[key])
    if len(doneList) == int(height)*int(width)-int(numBombs):
        messagebox.showinfo('Minesweeper','Congratulations -- you won!', parent=self)
        self.winner = True

def CheckLoss(self):
    '''Checks if player lost'''
    self.loser = True
    self.flagTrack['text'] = '0'
    messagebox.showerror('Minesweeper','KABOOM! You lose.', parent=self)
    for key in self.cells.keys():
        if self.cells[key].value == 9:
            self.cells[key].flagged = False
            self.cells[key].expose()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Lionblaze16
  • 23
  • 1
  • 7
  • 1
    In the future, don't post all of your code; just post an [MCVE](http://stackoverflow.com/help/mcve), just enough code to successfully demonstrate your problem. For example, a 5-line program that starts up an empty window and then tries to call `messagebox.showerror()` would show us exactly the same thing, but we'd know absolutely for sure that the problem is somewhere in those 5 lines. – abarnert Aug 13 '14 at 04:43

1 Answers1

2

If I lose, it will show an error message that says messagebox (a tkinter built in function) is undefined.

messagebox is not a function in tkinter, it's a module. And doing from pkg import * does not import submodules of pkg, just things defined directly in pkg.

So, you probably want to do this:

from tkinter import messagebox

(By the way, this is one of the reasons from foo import * can be confusing, but not the only one. That's why it's not recommended, except for playing around in the interactive interpreter, or in a few special cases.)

abarnert
  • 354,177
  • 51
  • 601
  • 671