如何在 tkinter 中制作一条跟随鼠标的线(它仅按方向跟随并且不改变它的大小)
How to make a line that follows the mouse(It follows it only by direction and don't change it's size)in tkinter
https://www.youtube.com/watch?v=TOEi6T2mtHo&ab_channel=TheCodingTrain
18:09
如何在 Tkinter 中做同样的事情
我试过的
from multiprocessing.connection import wait
from pickle import PicklingError
from tkinter import *
import win32api
from sklearn import preprocessing
root = Tk()
canvas = Canvas(root,bg = "pink",height = "500" ,width="1000")
def UpdateLine():
position = win32api.GetCursorPos()
x = position[0]
y = position[1]
wx = root.winfo_x()
wy = root.winfo_y()
if x < wx +1000 and x > wx and y < wy + 500 and y> wy:
canvas.coords(Line1, 100, 200,x-100 + 3 ,y-200)
root.after(1,UpdateLine)
def Create_Line(x, y, r, canvasName): #center coordinates, radius
position = win32api.GetCursorPos()
x = position[0]
y = position[1]
return canvasName.create_line(100,200,x,y)
Line1 = Create_Line(1,2,3,canvas)
canvas.pack()
root.after(1,UpdateLine)
root.mainloop()
我试过计算线之间的距离,而不是做一些糟糕的数学运算,但它仍然不起作用(不是我想要的那样)
为了让事情更清楚,我想将线旋转到鼠标(只是旋转没有别的因为我已经可以让它旋转但是当它旋转时线的大小会随之改变)
更具体些
如何制作 lookAt 函数(它使对象从字面上看在另一个对象中)
解决方案
- 求出鼠标的x,y坐标
- 计算鼠标方向的单位向量
- 给向量一个恒定的大小
示例
import tkinter as tk
root = tk.Tk()
cv = tk.Canvas(root, width=500, height=500)
cv.pack()
length = 100
def redraw(event):
cv.delete("all")
msx = event.x - 250
msy = event.y - 250
mag = (msx*msx + msy*msy) ** 0.5
print(250, 250, (msx/mag*length)+250, (msy/mag*length)+250)
cv.create_line(250, 250, (msx/mag*length)+250, (msy/mag*length)+250, fill="red")
cv.bind("<Motion>", redraw)
root.mainloop()
https://www.youtube.com/watch?v=TOEi6T2mtHo&ab_channel=TheCodingTrain
18:09 如何在 Tkinter 中做同样的事情
我试过的
from multiprocessing.connection import wait
from pickle import PicklingError
from tkinter import *
import win32api
from sklearn import preprocessing
root = Tk()
canvas = Canvas(root,bg = "pink",height = "500" ,width="1000")
def UpdateLine():
position = win32api.GetCursorPos()
x = position[0]
y = position[1]
wx = root.winfo_x()
wy = root.winfo_y()
if x < wx +1000 and x > wx and y < wy + 500 and y> wy:
canvas.coords(Line1, 100, 200,x-100 + 3 ,y-200)
root.after(1,UpdateLine)
def Create_Line(x, y, r, canvasName): #center coordinates, radius
position = win32api.GetCursorPos()
x = position[0]
y = position[1]
return canvasName.create_line(100,200,x,y)
Line1 = Create_Line(1,2,3,canvas)
canvas.pack()
root.after(1,UpdateLine)
root.mainloop()
我试过计算线之间的距离,而不是做一些糟糕的数学运算,但它仍然不起作用(不是我想要的那样) 为了让事情更清楚,我想将线旋转到鼠标(只是旋转没有别的因为我已经可以让它旋转但是当它旋转时线的大小会随之改变) 更具体些 如何制作 lookAt 函数(它使对象从字面上看在另一个对象中)
解决方案
- 求出鼠标的x,y坐标
- 计算鼠标方向的单位向量
- 给向量一个恒定的大小
示例
import tkinter as tk
root = tk.Tk()
cv = tk.Canvas(root, width=500, height=500)
cv.pack()
length = 100
def redraw(event):
cv.delete("all")
msx = event.x - 250
msy = event.y - 250
mag = (msx*msx + msy*msy) ** 0.5
print(250, 250, (msx/mag*length)+250, (msy/mag*length)+250)
cv.create_line(250, 250, (msx/mag*length)+250, (msy/mag*length)+250, fill="red")
cv.bind("<Motion>", redraw)
root.mainloop()