【问题标题】:How to replace draggable canvas with a JPEG image in Tkinter?如何在 Tkinter 中用 JPEG 图像替换可拖动画布?
【发布时间】:2019-10-24 05:48:37
【问题描述】:

代码

from tkinter import *

root = Tk()
root.state('zoomed')


def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)



card = Canvas(root, width=50, height=50, bg='blue')
card.place(x=300, y=600,anchor=CENTER)


card.bind("<B1-Motion>", drag)


root.mainloop()

问题

我怎样才能用特定的JPEG图像文件代替可移动的蓝色方块?

但不给它宽度和高度,不管图像的高度和宽度是多少,保持原样。

我的意思是,自动加载原始大小的图像

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    只需使用Label 小部件:

    from tkinter import *
    
    root = Tk()
    root.state('zoomed')
    
    def drag(event):
        event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)
    
    pic = PhotoImage(file="pic_file_name")
    
    label = Label(root,image=pic)
    label.place(x=300, y=600,anchor=CENTER)
    label.img = pic
    
    label.bind("<B1-Motion>", drag)
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多