在 Python Tkinter 窗口中显示 Google Map API [英] Display Google Map API in Python Tkinter window

查看:34
本文介绍了在 Python Tkinter 窗口中显示 Google Map API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 Python 开发 Google Map API.我正在使用可以在此网站

此代码在编译时会生成一个htm"文件,显示 Google 地图,标记放置在地图上.

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

from Tkinter import * # 顺序似乎很重要:先导入 Tkinterimport Image, ImageTk # 然后导入 ImageTk类 MyFrame(Frame):def __init__(self, master, im):Frame.__init__(self, master)self.caption = Label(self, text="关于地图的一些文字")self.caption.grid()self.image = ImageTk.PhotoImage(im) # <--- PhotoImage() 的结果必须被存储self.image_label = Label(self, image=self.image, bd=0) # <--- 如果 'image = ImageTk.PhotoImage(im)' 将不起作用self.image_label.grid()self.grid()im = Image.open("test.html") # 从磁盘读取地图# 或者您可以使用您通过 URL 请求中的选项 2 直接创建的 PIL 图像...mainw = Tk()mainw.frame = MyFrame(mainw, im)mainw.mainloop()

使用该窗口框架,我想在该窗口框架中显示 Google 地图的htm"图像.

解决方案

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

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

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

导入浏览器webbrowser.open('test.htm')

就是这样.

Hi I am working on Google Map API in Python. I am using the source code which can be found at this website

This code when compiled produces a 'htm' file showing a Google Map with the markers placed on the map.

So I have created a Window Frame shown below:

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

And with that Window Frame I want to display the 'htm' image of the Google Map in that Window Frame.

解决方案

The htm image pymaps produces isn't an image, it's an html file. Basically a little webpage. To display it, you would have to render the html. The only html renderer for TkInter that I know of is TkHTML, although I've never used it, so it might not support all the javascript that you html file uses.

You would be much better off dropping TkInter entirely and switching to a more modern widget toolkit such as wxPython which has html rendering built in. You can see the documentation for html in wxPython here. If you have GTK on your system, I've used pywebkitgtk successfully.

However, do you need to render this frame for something specific? If you just want to open the file from python, you can use the webbrowser built in library to open it with your default browser.

import webbrowser

webbrowser.open('test.htm')

And that's it.

这篇关于在 Python Tkinter 窗口中显示 Google Map API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆