英文:
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("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()
Thanks for helping
答案1
得分: -1
在第30行添加这行代码。
self.canvas.grid()
英文:
Add this line line 30.
self.canvas.grid()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论