英文:
Canvas doesnt draw line correctly because of method problem
问题
以下是您要求的翻译内容:
"start" 和 "clear line" 方法只在我的鼠标光标在 tkinter 窗口而不是画布上时起作用
但问题是要在画布上画线。为什么会这样?
尝试将方法参数改为 (self.canvas) 而不是 (self) 但没有用,方法上没有太多可以改的了,所以我不知道
我是初学者,请提供示例
英文:
The methods "start" and "clear line" only work when my mouse cursor is on the tkinter window not the canvas
But the point is to draw line on the canvas. How it is this way?
Tried to put as method argument (self.canvas) instead of (self) but it did not work and there is not much left to change on the method so i dont know
I am beginner please provide example
import tkinter as tk
size = [960,800]
line_id = None
line_points = []
line_options = {}
class Window(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
self.master.title("Calcul mental")
self.master.minsize(size[0],size[1])
self.grid(sticky=tk.E+tk.W+tk.N+tk.S)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
global line_id, line_points, line_options
self.canvas = tk.Canvas( self,border=5,bg = "white", width=size[0], height=size[1]/2 )
self.canvas.grid()
self.bind('<Button-1>', self.set_start)
self.bind('<B1-Motion>', self.draw_line)
self.bind('<ButtonRelease-1>', self.end_line)
self.bind('<Button-3>', self.clear_line)
def draw_line(self,event):
global line_id,line_points,line_options
print("draw")
line_points.extend((event.x, event.y))
line_id = self.canvas.create_line(line_points, **line_options,tags="draw")
def set_start(self,event):
global line_points
print("start")
line_points.extend((event.x, event.y))
def end_line(self,event=None):
global line_id
print('end')
line_points.clear()
line_id = None
def clear_line(self,event):
print("clear line")
self.canvas.delete("draw")
Window().mainloop()
答案1
得分: 1
Thanks for the help @Bryan Oakley and @acw1668 =)
I changed:
self.bind('<Button-1>', self.set_start)
self.bind('<B1-Motion>', self.draw_line)
self.bind('<ButtonRelease-1>', self.end_line)
self.bind('<Button-3>', self.clear_line)
to:
self.canvas.bind('<Button-1>', self.set_start)
self.canvas.bind('<B1-Motion>', self.draw_line)
self.canvas.bind('<ButtonRelease-1>', self.end_line)
self.canvas.bind('<Button-3>', self.clear_line)
and it works.
英文:
Thanks for the help @Bryan Oakley and @acw1668 =)
i changed
self.bind('<Button-1>', self.set_start)
self.bind('<B1-Motion>', self.draw_line)
self.bind('<ButtonRelease-1>', self.end_line)
self.bind('<Button-3>', self.clear_line)
to
self.canvas.bind('<Button-1>', self.set_start)
self.canvas.bind('<B1-Motion>', self.draw_line)
self.canvas.bind('<ButtonRelease-1>', self.end_line)
self.canvas.bind('<Button-3>', self.clear_line)
and it works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论