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函式(它使物件從字面上看另一個物件)
uj5u.com熱心網友回復:
解決方案
- 找到滑鼠的 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.uj5u.com/qita/468256.html
