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()
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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.