Search code examples
pythontkintermessagebox

Why does trying to create a tkinter messagebox cause a 'module not callable' error?


In this, when I run it and press the 'yes' option on wanting to play, I get an error that says

line 36, in <module>
    tkinter.messagebox("Snake","Have fun!")
TypeError: 'module' object is not callable

I'm not sure why I'm getting this error as I have imported the messagebox module multiple times and it still doesn't work. can anyone offer improvements or an explanation?

Code:

#importing modules that I need
import tkinter                      
from tkinter import *
from msvcrt import *
#importing messagebox module separately
from tkinter import messagebox      


#the function that closes the window
def closeitall(self):               
    board.destroy()

#creating the game board
board=tkinter.Tk()                 

#setting background colour
board.configure(background="#B3C9D0")

#sets title of board window
board.title("Snake")

#sets size of board
board.geometry("700x500")               

#makes it so that the board can't be resized
board.resizable(0,0)                    

#asks question
play=messagebox.askquestion("Snake","Do you want to play snake?")   

#closes window if answer is no
if play=="no":
    closeitall(board)
#creates a messagebox 
else:
    tkinter.messagebox("Snake","Have fun!")

#closes board when escape key pressed
board.bind("<Escape>",closeitall)           

board.mainloop()

Solution

  • You should use something like

    tkinter.messagebox.showinfo("Snake","Have fun!")
    

    cause messagebox is a module and not a function.