【问题标题】:Draw line on image in tkinter在 tkinter 中的图像上画线
【发布时间】:2015-01-04 21:32:03
【问题描述】:

我正在尝试制作一个脚本,该脚本将在 python GUI 中的图像上绘制线条。我已经能够在 GUI 上获得图像,但不知道如何绘制附加线。脚本应该可以循环,这样我就可以画更多的线了。

到目前为止我所拥有的:

import tkinter as Tk

root = Tk.Tk()
background_image=Tk.PhotoImage(file="map.png")
background_label = Tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.wm_geometry("794x370")
root.title('Map')
root.mainloop()

【问题讨论】:

    标签: python tkinter python-3.4 tkinter-canvas


    【解决方案1】:

    您可以通过首先将图像放在画布上来做到这一点:

    import tkinter as Tk
    
    root = Tk.Tk()
    canvas = Tk.Canvas(root)
    background_image=Tk.PhotoImage(file="map.png")
    canvas.pack(fill=Tk.BOTH, expand=1) # Stretch canvas to root window size.
    image = canvas.create_image(0, 0, anchor=Tk.NW, image=background_image)
    line = canvas.create_line(10, 10, 100, 35, fill="red")
    root.wm_geometry("794x370")
    root.title('Map')
    root.mainloop()
    

    【讨论】:

    • 请注意,这将失败,因为 Tkinter.PhotoImage 无法处理 png。使用 GIF 就可以了。
    • @IngemarRagnemalm 对于带有 Tk 8.5 的 Tkinter 来说确实如此。当与 Tk 8.6 一起使用时,Tkinter 开箱即用地支持 PNG。
    猜你喜欢
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多