[英]Tkinter GUI Image w/ Button
我目前正在使用Tkinter制作带有BMP图像的交互式GUI。 本质上,我希望能够单击一个按钮,然后该按钮允许用户指定图像上的任何三个点,然后将生成最佳拟合的椭圆并返回椭圆内的最小值。
我能够产生BMP图像,但是我不知道如何产生带有图像的按钮。 我将如何去做?
单击按钮后,它将执行将鼠标光标更改为十字准线的功能,然后在单击时获取图像上的任意三个点,并将其保存到变量中,稍后我将对其进行操作以产生椭圆形。 我希望这些点击可能会产生一个红点来表示其位置。
这是我的代码:
from tkinter import *
from PIL import Image, ImageTk
def bmpGUI():
top=Tk()
top.title("BMP Image")
image = Image.open("ap41.ddr.brf.sdat.bmp")
widthBMP, heightBMP = image.size
tkimage = ImageTk.PhotoImage(image)
w = Canvas(top, width=widthBMP+200, height=heightBMP)
w.create_image((widthBMP/2,heightBMP/2),image=tkimage)
w.pack()
top.mainloop()
return
def main():
bmpGUI()
if __name__ == "__main__":
main()
我在宽度上添加了200px,以表明我想要在图像旁边添加一个交互式边栏。
我建议为画布使用比w
更有用的变量名,同样为了使它起作用,需要将画布设为全局
tkimage.configure(cursor="plus")
w.bind("<Button-1>", imgClick)
pos = []
def imgClick(e):
x=w.canvasx(e.x)
y=w.canvasy(e.y)
pos.append((x,y))
w.create_line(x-5,y,x+5,y, fill="red", tags="crosshair")
w.create_line(x,y-5,x,y+5, fill="red", tags="crosshair")
当您拥有所有3个职位时,您只需使用w.delete("crosshair")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.