gpt4 book ai didi

Python Tkinter 连续线与端点不匹配

转载 作者:行者123 更新时间:2023-11-30 23:52:05 26 4
gpt4 key购买 nike

基本上,我正在尝试创建一条连续的线(从用户单击时开始,在右键单击时结束),并且我让它工作,但由于某种原因,端点(椭圆形)并没有真正对齐与线的两侧完美契合(当你尝试用多个点画一条直线或尝试直角时,这一点变得非常明显)...我已经尝试了几乎所有方法,但感到非常沮丧,所以我在这里发帖看看你们是否有什么想法..

这是代码,您可以尝试一下并了解我的意思:

from Tkinter import *

class GUI:
def __init__(self,root):
Window = Frame(root)
self.DrawArea = Canvas(Window)
self.DrawArea.pack()
Window.pack()

self.linewidth = 20

self.DrawArea.config(cursor="crosshair")
self.DrawArea.bind("<Button 1>",self.startline)


def startline(self,event):
self.startx, self.starty = self.DrawArea.canvasx(event.x),self.DrawArea.canvasy(event.y)

xa,ya = self.startx-self.linewidth/2 , self.starty+self.linewidth/2
xb,yb = self.startx+self.linewidth/2 , self.starty-self.linewidth/2

self.StartPoint = self.DrawArea.create_oval(xa,ya,xb,yb,fill='red',outline='')
self.Line = None
self.EndPoint = None

self.DrawArea.bind("<Motion>",self.updateline)
self.DrawArea.bind("<Button 3>",self.killline)

def updateline(self,event):
if self.EndPoint: self.DrawArea.delete(self.EndPoint)
if self.Line: self.DrawArea.delete(self.Line)

x,y = self.DrawArea.canvasx(event.x),self.DrawArea.canvasy(event.y)
xa,ya = x-self.linewidth/2 , y+self.linewidth/2
xb,yb = x+self.linewidth/2 , y-self.linewidth/2

self.Line = self.DrawArea.create_line(self.startx,self.starty,x,y,fill='red',width=self.linewidth)
self.EndPoint = self.DrawArea.create_oval(xa,ya,xb,yb,fill='red',outline='')

def killline(self,event):
self.DrawArea.delete(self.Line)
self.DrawArea.delete(self.StartPoint)
self.DrawArea.delete(self.EndPoint)
self.DrawArea.unbind("<Motion>")


if __name__ == '__main__':
root = Tk()
root.title("Line Draw")
App = GUI(root)
root.mainloop()

最佳答案

您可以使用capstyle,而不是在行尾绘制圆圈。和 joinstyle line 的选项.

例如:

self.Line = self.DrawArea.create_line(self.startx,self.starty, x, y, fill='red',
width=self.linewidth, capstyle=ROUND, joinstyle=ROUND)

关于Python Tkinter 连续线与端点不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6459937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com