[英]Problems while drawing straight line and dragging in Tkinter canvas
So, I'm trying to draw vertical lines in canvas on the click of the button "line".所以,我试图通过单击“线”按钮在 canvas 中绘制垂直线。
These are the problems and my requirements:这些是问题和我的要求:
This is the code i've written:这是我写的代码:
from tkinter import *
root = Tk()
canvas = tkinter.Canvas(root, width = 480,height = 600)
canvas.pack()
def draw_lines():
canvas.create_line(300, 35, 300, 200, dash=(4, 2))
def drag(event):
event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)
canvas.bind("<B1-Motion>", drag)
btn1 = Button(root, text = 'line', bd = '5',command = draw_lines)
btn2 = Button(root, text = 'Close', bd = '5',command = root.destroy)
btn1.pack(side = 'top')
btn2.pack(side = 'top')
canvas.mainloop()
please help!!!!请帮忙!!!!
Using Python 3.11.0rc1.使用 Python 3.11.0rc1。
I added:我补充说:
geometry
. geometry
。 so it will not resize when dragging.Canvas
. Canvas
。 You have to play around with height set to 800bd=5
.bd=5
。 You can suit yourself.btn1.pack(side=TOP)
btn1.pack(side=TOP)
的引号canvas.pack()
always next to mainloop()
canvas.pack()
总是在mainloop()
() 旁边Here is the code re-worked:这是重新工作的代码:
from tkinter import *
root = Tk()
root.geometry('400x650')
canvas = Canvas(root, width=480, height=600)
def draw_lines():
canvas.create_line(300, 35, 300, 200, dash=(4, 2))
def drag(event):
event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)
canvas.bind("<B1-Motion>", drag)
btn1 = Button(root, text='line', command=draw_lines)
btn2 = Button(root, text= 'Close', command=root.destroy)
btn1.pack(side=TOP)
btn2.pack(side=TOP)
canvas.pack()
canvas.mainloop()
Result output:结果 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.