基本程序不会编译

时间:2015-02-19 23:51:55

标签: python wxpython pycharm mapnik

我在Windows 7上使用pycharm(python)(和mapnik),我只是想在安装后测试一切是否到位。我在网上使用了一个例子就是它,我有一个帧错误。这可能是安装问题吗?编译??我对python很新。提前感谢您的时间。

"""
This is a simple wxPython application demonstrates how to
integrate mapnik, it do nothing but draw the map from the World Poplulation XML
example:
https://github.com/mapnik/mapnik/wiki/GettingStartedInXML

Victor Lin. (bornstub@gmail.com)
Blog http://blog.ez2learn.com

"""

import mapnik
import wx


class Frame(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, size=(800, 500) ,*args, **kwargs)
            self.Bind(wx.EVT_PAINT, self.onPaint)

            self.mapfile = "population.xml"
            self.width = 800
            self.height = 500
            self.createMap()
            self.drawBmp()

        def createMap(self):
            """Create mapnik object

            """
            self.map = mapnik.Map(self.width, self.height)
            mapnik.load_map(self.map, self.mapfile)
            bbox = mapnik.Envelope(mapnik.Coord(-180.0, -75.0), mapnik.Coord(180.0, 90.0))
            self.map.zoom_to_box(bbox)

        def drawBmp(self):
            """Draw map to Bitmap object

            """
            # create a Image32 object
            image = mapnik.Image(self.width, self.height)
            # render map to Image32 object
            mapnik.render(self.map, image)
            # load raw data from Image32 to bitmap
            self.bmp = wx.BitmapFromBufferRGBA(self.width, self.height, image.tostring())

        def onPaint(self, event):
            dc = wx.PaintDC(self)
            memoryDC = wx.MemoryDC(self.bmp)
            # draw map to dc
            dc.Blit(0, 0, self.width, self.height, memoryDC, 0, 0)

        if __name__ == '__main__':

            app = wx.App()
        frame = frame(None, title="wxPython Mapnik Demo")
        frame.Show()
        app.MainLoop()

这是错误消息:

 Traceback (most recent call last):
  File "C:/Python27/example.py", line 16, in <module>
    class Frame(wx.Frame):
  File "C:/Python27/example.py", line 56, in Frame
    frame = frame(None, title="wxPython Mapnik Demo")
NameError: name 'frame' is not defined

Process finished with exit code 1

2 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。这一行不正确:

frame = frame(None, title="wxPython Mapnik Demo")

应该是:

frame = Frame(None, title="wxPython Mapnik Demo")

原因是类名是Frame(不是大写 F ),并且您希望实例化该类以运行该程序。另请注意,行if __name__ == '__main__':缩进不正确。以下完整示例应该有效:

import mapnik
import wx


class Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, size=(800, 500) ,*args, **kwargs)
        self.Bind(wx.EVT_PAINT, self.onPaint)

        self.mapfile = "population.xml"
        self.width = 800
        self.height = 500
        self.createMap()
        self.drawBmp()

    def createMap(self):
        """Create mapnik object

        """
        self.map = mapnik.Map(self.width, self.height)
        mapnik.load_map(self.map, self.mapfile)
        bbox = mapnik.Envelope(mapnik.Coord(-180.0, -75.0), mapnik.Coord(180.0, 90.0))
        self.map.zoom_to_box(bbox)

    def drawBmp(self):
        """Draw map to Bitmap object

        """
        # create a Image32 object
        image = mapnik.Image(self.width, self.height)
        # render map to Image32 object
        mapnik.render(self.map, image)
        # load raw data from Image32 to bitmap
        self.bmp = wx.BitmapFromBufferRGBA(self.width, self.height, image.tostring())

    def onPaint(self, event):
        dc = wx.PaintDC(self)
        memoryDC = wx.MemoryDC(self.bmp)
        # draw map to dc
        dc.Blit(0, 0, self.width, self.height, memoryDC, 0, 0)

if __name__ == '__main__':

    app = wx.App()
    frame = Frame(None, title="wxPython Mapnik Demo")WAS WRONG
    frame.Show()
    app.MainLoop()

答案 1 :(得分:0)

我认为越来越好,我得到了一个不同的错误:

    C:\Python27\python.exe C:/Python27/text.py
Traceback (most recent call last):
  File "C:/Python27/text.py", line 45, in <module>
    frame = Frame(None, title="wxPython Mapnik Demo")
  File "C:/Python27/text.py", line 13, in __init__
    self.createMap()
  File "C:/Python27/text.py", line 21, in createMap
    mapnik.load_map(self.map, self.mapfile)
RuntimeError: Could not load map file: File does not exist       of 'population.xml'

现在什么是population.xml文件??

相关问题