如何在Tkinter中使用Canvas绘制点? [英] How can I draw a point with Canvas in Tkinter?

查看:744
本文介绍了如何在Tkinter中使用Canvas绘制点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Tkinter中绘制一个点,现在我正在使用 Canvas 进行绘制,但是我没有找到在<$$中绘制这种点的方法c $ c> Canvas 类。 Canvas 提供了一种名为 crete_line(x1,y1,x2,y2)的方法,所以我尝试设置 x1 = x2,y1 = y2 来画点,但这不起作用。

I want to draw a point in Tkinter,Now I'm using Canvas to make it,but I didn't find such method to draw a point in Canvas class.Canvas provides a method called crete_line(x1,y1,x2,y2),so i tried to set x1=x2,y1=y2 to draw a point, but it doesn't work.

所以任何人都可以告诉我如何制作,最好使用 Canvas 制作,其他解决方案也可以接受。谢谢!

So anyone can tell me how to make it,it will be better if use Canvas can make it,other solution will be also accepted.Thanks!

推荐答案

没有方法直接将点放在画布上。下面的方法使用 create_oval 方法显示点。

There is no method to directly put a point on Canvas. The method below shows points using create_oval method.

尝试以下操作:

from Tkinter import *

canvas_width = 500
canvas_height = 150


def paint(event):
    python_green = "#476042"
    x1, y1 = (event.x - 1), (event.y - 1)
    x2, y2 = (event.x + 1), (event.y + 1)
    w.create_oval(x1, y1, x2, y2, fill=python_green)



master = Tk()
master.title("Points")
w = Canvas(master,
           width=canvas_width,
           height=canvas_height)
w.pack(expand=YES, fill=BOTH)
w.bind("<B1-Motion>", paint)

message = Label(master, text="Press and Drag the mouse to draw")
message.pack(side=BOTTOM)

mainloop()

这篇关于如何在Tkinter中使用Canvas绘制点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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