【问题标题】:To display shapefile in tkinter?在 tkinter 中显示 shapefile?
【发布时间】:2019-04-01 07:03:02
【问题描述】:

我希望代码中下面提到的“shapefile”显示在 tkinter 窗口的“sideFrame”内。但是现在,shapefile 正在另一个我不想打开的窗口中打开。也就是说,我想在“tkinter”窗口的右框架内显示“shapefile”。我是这个领域的新手,所以可以通过代码解释清楚。

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("TkAgg") # for backend
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

def forSubmit():
    if MyVar1.get()== 0 or MyVar2.get()==0:
        messagebox.showwarning("Warning", "Select input files!")
    fp = r"F:\ISRO\Spatial_Data\grid_jagalur_spatialjoin.shp"
    data = gpd.read_file(fp)
    print (type(data))
    data.head()
    data.plot()
    plt.show(sideFrame)                                                                         

middleFrame = LabelFrame(root,width=800,text="Input data")
middleFrame.grid(row=1, column=0, padx=8, pady=8, sticky=N+S+W+E)
middleFrame.grid_rowconfigure(0, weight=1)
middleFrame.grid_rowconfigure(1, weight=0)
middleFrame.grid_columnconfigure(0, weight=1)
middleFrame.grid_columnconfigure(1, weight=1)      

Button6 = Button(middleFrame, text="View", command=forSubmit)
Button6.grid(row=1, column=1, padx=4, pady=4,sticky=E) 

sideFrame = LabelFrame(root,width=500, bg="powder blue", text="Image")
sideFrame.grid(row=0, column=1, padx=8, pady=8, sticky=N+S+W+E, rowspan=3)
sideFrame.grid_rowconfigure(0, weight=1)
sideFrame.grid_rowconfigure(1, weight=1)
sideFrame.grid_rowconfigure(2, weight=1)
sideFrame.grid_columnconfigure(0, weight=1)
sideFrame.grid_columnconfigure(1, weight=1)

【问题讨论】:

  • 你能把你的代码缩减成minimal reproducible example吗?
  • 我已经编辑了这个问题。你能检查一下吗@Haem
  • 这样更好。
  • 好吧..我可以得到解决方案..@Haem
  • 我不太了解 matplotlib 或 tkinter,我只能建议您如何改进您的问题,以便有人可以更轻松地解决您的问题。

标签: python-3.x matplotlib tkinter tkinter-canvas


【解决方案1】:

您应该将一个 FigureCanvasTkAgg 小部件添加到您的 sideFrame 中,然后尝试以下几行:

fig, ax = plt.subplots(nrows = 1, ncols = 1)
cavnas_plot = FigureCanvasTkAgg(fig, master = sideFrame)
canvas_plot.draw()
canvas_plot.get_tk_widget.grid(row = 0, column = 0)

确保有足够的空间让 sideFrame 在主窗口中可见

【讨论】:

  • 不要在自定义画布中使用 pyplot 图形。请参阅documentation example(注意它根本不使用pyplot)。此外,这对于问题中的问题可能不是很有帮助,该问题询问绘制 shapefile。
  • @ImportanceOfBeingErnest 您能解释一下为什么在自定义画布中使用 pyplot 图形是错误的吗?我可能在文档示例中遗漏了一些东西。但是,我已经在几个脚本中使用过它并且它可以工作。
  • 如果您的图形是由 pyplot 以及您的自定义 GUI 管理的,则不可避免地会导致冲突。如果你知道自己在做什么或者很幸运,你可能不会遇到这样的冲突。文档示例不使用pyplot。所以如果你坚持下去,你就安全了。
  • @ImportanceOfBeingErnest 据我所知,自定义 GUI 与图形的唯一交互是在 draw 处,仅使用渲染器绘制图形。而用来绘制的图是由pyplot提供的。如果我遗漏了什么,请纠正我
  • 它不是由pyplot“提供”的,但pyplot仍然管理这个数字。就像父母离婚后带着孩子一样。父母双方都管理着孩子,如果他们想去度假,各自在不同的国家,孩子最终会去哪里?
猜你喜欢
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 2018-03-05
相关资源
最近更新 更多