Answer the question
In order to leave comments, you need to log in
How to draw lines in tkinter?
I want to draw lines in tkinter. If I quickly draw with this code, then there is space between the circles. I know that I need to generate lines between these circles, but for this I need the previous coordinates of the circle and I do not know how these coordinates can be obtained. How can I do that?
from tkinter import *
root = Tk()
canv = Canvas(bg="white", width=1000,height=1000)
canv.pack()
def onmotion(event):
canv.create_oval((event.x, event.y, event.x+20, event.y+20), width=0, fill='black')
#canv.create_line(?, ?, event.x+10, event.y+10,width=20, fill='black')
canv.bind("<B1-Motion>", onmotion)
root.mainloop()
Answer the question
In order to leave comments, you need to log in
I managed. True, the code is written haphazardly, but it works. Even better lines can be achieved using smooth (smoothing the line), but for this, in addition to the last and penultimate coordinates, the penultimate coordinates are also needed
from tkinter import *
root = Tk()
canv = Canvas(bg="white", width=1000,height=1000)
canv.pack()
def click(event):
canv.create_oval((event.x, event.y, event.x+20, event.y+20), width=0, fill='black')
xp = event.x
yp = event.y
xp = str(xp)+'\n'
yp = str(yp)
f = open('tt.txt','w')
xp = f.write(xp)
yp = f.write(yp)
f.close
canv.bind("<B1-Motion>", onmotion)
def onmotion(event):
f = open(r'tt.txt')
xp = f.readlines()
xp[0] = int(xp[0])+10
xp[1] = int(xp[1])+10
f.close
canv.create_oval((event.x, event.y, event.x+20, event.y+20), width=0, fill='black')
canv.create_line(xp[0], xp[1], event.x+10, event.y+10,width=20, fill='black')
xp = event.x
yp = event.y
xp = str(xp)+'\n'
yp = str(yp)
f = open('tt.txt','w')
xp = f.write(xp)
yp = f.write(yp)
f.close
canv.bind("<Button-1>", click)
root.mainloop()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question