Canvas由于方法问题未正确绘制线条。

huangapple go评论63阅读模式
英文:

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(&#39;&lt;Button-1&gt;&#39;, self.set_start)
self.bind(&#39;&lt;B1-Motion&gt;&#39;, self.draw_line)
self.bind(&#39;&lt;ButtonRelease-1&gt;&#39;, self.end_line)
self.bind(&#39;&lt;Button-3&gt;&#39;, self.clear_line)

to

self.canvas.bind(&#39;&lt;Button-1&gt;&#39;, self.set_start)
self.canvas.bind(&#39;&lt;B1-Motion&gt;&#39;, self.draw_line)
self.canvas.bind(&#39;&lt;ButtonRelease-1&gt;&#39;, self.end_line)
self.canvas.bind(&#39;&lt;Button-3&gt;&#39;, self.clear_line)

and it works

huangapple
  • 本文由 发表于 2023年5月21日 09:07:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定