Canvas内部的tkinter不画线。

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

Canvas inside tkinter dont draw line

问题

I want to draw line when my left mouse button is pressed.

It worked when I did it on another file outside of the "Window" class, but now it doesn't.

The prints tell me the methods are still working.

I am a beginner; please provide an 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.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()
英文:

I want to draw line when my left mouse button is pressed

It worked when i did it on another file outside of the "Window" class but now it doesnt

the prints tell me the methods are still working

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(&quot;Calcul mental&quot;)
        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.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)
        

    def draw_line(self,event):
        global line_id,line_points,line_options
        print(&quot;draw&quot;)
        line_points.extend((event.x, event.y))
        line_id = self.canvas.create_line(line_points, **line_options,tags=&quot;draw&quot;)
        
    def set_start(self,event):
        global line_points
        print(&quot;start&quot;)
        line_points.extend((event.x, event.y))
        
    def end_line(self,event=None):
        global line_id
        print(&#39;end&#39;)
        line_points.clear()
        line_id = None
        
    def clear_line(self,event):
        print(&quot;clear line&quot;)
        self.canvas.delete(&quot;draw&quot;)

Window().mainloop()

Thanks for helping

答案1

得分: -1

在第30行添加这行代码。

self.canvas.grid()
英文:

Add this line line 30.

self.canvas.grid()

huangapple
  • 本文由 发表于 2023年5月20日 23:18:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295936.html
匿名

发表评论

匿名网友

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

确定