在 Python Tkinter 窗口中显示 Google Map API

use*_*252 6 python tkinter

大家好,我正在用 Python 开发 Google Map API。我正在使用可以在这个网站上找到的源代码

此代码编译后会生成一个“htm”文件,显示 Google 地图,并在地图上放置标记。

所以我创建了一个如下所示的窗框:

from Tkinter import *  # order seems to matter: import Tkinter first
import Image, ImageTk  # then import ImageTk
class MyFrame(Frame):
    def __init__(self, master, im):
        Frame.__init__(self, master)
        self.caption = Label(self, text="Some text about the map")
        self.caption.grid()
        self.image = ImageTk.PhotoImage(im) # <--- results of PhotoImage() must be stored
        self.image_label = Label(self, image=self.image, bd=0) # <--- will not work if 'image = ImageTk.PhotoImage(im)'
    self.image_label.grid()
    self.grid()

im = Image.open("test.html") # read map from disk

# or you could use the PIL image you created directly via option 2 from the URL request ...
mainw = Tk()
mainw.frame = MyFrame(mainw, im)
mainw.mainloop()
Run Code Online (Sandbox Code Playgroud)

通过该窗口框架,我想在该窗口框架中显示 Google 地图的“htm”图像。

Nar*_*lei 5

pymaps 生成的 htm 图像不是图像,而是 html 文件。基本上就是一个小网页。要显示它,您必须渲染 html。我所知道的 TkInter 的唯一 html 渲染器是TkHTML,尽管我从未使用过它,所以它可能不支持 html 文件使用的所有 javascript。

您最好完全放弃 TkInter 并切换到更现代的小部件工具包,例如内置了 html 渲染的 wxPython。您可以在此处查看 wxPython 中的 html 文档。如果你的系统上有 GTK,我已经成功使用了pywebkitgtk

但是,您是否需要为特定的内容渲染此帧?如果您只想从 python 打开文件,您可以使用内置库的webbrowser用默认浏览器打开它。

import webbrowser

webbrowser.open('test.htm')
Run Code Online (Sandbox Code Playgroud)

就是这样。