为什么尝试创建tkinter消息框会导致模块无法调用'错误?

时间:2017-11-16 15:20:04

标签: python tkinter messagebox

在此,当我运行它并按下'yes'选项想要播放时,我收到错误消息

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

我不确定为什么我收到此错误,因为我多次导入messagebox模块但仍然无法正常工作。任何人都可以提供改进或解释吗?

代码:

#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()

2 个答案:

答案 0 :(得分:3)

您应该使用类似

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

因为messagebox是一个模块,而不是一个函数。

答案 1 :(得分:2)

您正在使用messagebox功能。

它实际上是一个模块,因此您无法调用它。