I am making a physics simulator using tkinter and I want to have an image on top of a button in dnd, tkinter so that when I move the button, the image moves. How do I do this? The image and code are in the same folder.
I have tried adding an image by using it like a normal button but it gives an error. The code is the code from help(dnd) in python. I am using python 3.6. I need to identify the type of component the button is when I move it
Also, if anyone can tell me how to use collision detect to detect when 2 images are touching.
import tkinter
__all__ = ["dnd_start", "DndHandler"]
def dnd_start(source, event):
h = DndHandler(source, event)
if h.root:
return h
else:
return None
# The class that does the work
class DndHandler:
root = None
def __init__(self, source, event):
if event.num > 5:
return
root = event.widget._root()
try:
root.__dnd
return # Don't start recursive dnd
except AttributeError:
root.__dnd = self
self.root = root
self.source = source
self.target = None
self.initial_button = button = event.num
self.initial_widget = widget = event.widget
self.release_pattern = "<B%d-ButtonRelease-%d>" % (button, button)
self.save_cursor = widget['cursor'] or ""
widget.bind(self.release_pattern, self.on_release)
widget.bind("<Motion>", self.on_motion)
widget['cursor'] = "hand2"
def __del__(self):
root = self.root
self.root = None
if root:
try:
del root.__dnd
except AttributeError:
pass
def on_motion(self, event):
x, y = event.x_root, event.y_root
target_widget = self.initial_widget.winfo_containing(x, y)
source = self.source
new_target = None
while target_widget:
try:
attr = target_widget.dnd_accept
except AttributeError:
pass
else:
new_target = attr(source, event)
if new_target:
break
target_widget = target_widget.master
old_target = self.target
if old_target is new_target:
if old_target:
old_target.dnd_motion(source, event)
else:
if old_target:
self.target = None
old_target.dnd_leave(source, event)
if new_target:
new_target.dnd_enter(source, event)
self.target = new_target
def on_release(self, event):
self.finish(event, 1)
def cancel(self, event=None):
self.finish(event, 0)
def finish(self, event, commit=0):
target = self.target
source = self.source
widget = self.initial_widget
root = self.root
try:
del root.__dnd
self.initial_widget.unbind(self.release_pattern)
self.initial_widget.unbind("<Motion>")
widget['cursor'] = self.save_cursor
self.target = self.source = self.initial_widget = self.root = None
if target:
if commit:
target.dnd_commit(source, event)
else:
target.dnd_leave(source, event)
finally:
source.dnd_end(target, event)
class Icon:
def __init__(self, name):
self.name = name
self.canvas = self.label = self.id = None
def attach(self, canvas, x=10, y=10):
if canvas is self.canvas:
self.canvas.coords(self.id, x, y)
return
if self.canvas:
self.detach()
if not canvas:
return
label = tkinter.Label(canvas, text=self.name,
borderwidth=2, relief="raised")
id = canvas.create_window(x, y, window=label, anchor="nw")
self.canvas = canvas
self.label = label
self.id = id
label.bind("<ButtonPress>", self.press)
def detach(self):
canvas = self.canvas
if not canvas:
return
id = self.id
label = self.label
self.canvas = self.label = self.id = None
canvas.delete(id)
label.destroy()
def press(self, event):
if dnd_start(self, event):
# where the pointer is relative to the label widget:
self.x_off = event.x
self.y_off = event.y
# where the widget is relative to the canvas:
self.x_orig, self.y_orig = self.canvas.coords(self.id)
def move(self, event):
x, y = self.where(self.canvas, event)
self.canvas.coords(self.id, x, y)
def putback(self):
self.canvas.coords(self.id, self.x_orig, self.y_orig)
def where(self, canvas, event):
# where the corner of the canvas is relative to the screen:
x_org = canvas.winfo_rootx()
y_org = canvas.winfo_rooty()
# where the pointer is relative to the canvas widget:
x = event.x_root - x_org
y = event.y_root - y_org
# compensate for initial pointer offset
return x - self.x_off, y - self.y_off
def dnd_end(self, target, event):
pass
class Tester:
def __init__(self, root):
self.top = tkinter.Toplevel(root)
self.canvas = tkinter.Canvas(self.top, width=100, height=100)
self.canvas.pack(fill="both", expand=1)
self.canvas.dnd_accept = self.dnd_accept
def dnd_accept(self, source, event):
return self
def dnd_enter(self, source, event):
self.canvas.focus_set() # Show highlight border
x, y = source.where(self.canvas, event)
x1, y1, x2, y2 = source.canvas.bbox(source.id)
dx, dy = x2-x1, y2-y1
self.dndid = self.canvas.create_rectangle(x, y, x+dx, y+dy)
self.dnd_motion(source, event)
def dnd_motion(self, source, event):
x, y = source.where(self.canvas, event)
x1, y1, x2, y2 = self.canvas.bbox(self.dndid)
self.canvas.move(self.dndid, x-x1, y-y1)
def dnd_leave(self, source, event):
self.top.focus_set() # Hide highlight border
self.canvas.delete(self.dndid)
self.dndid = None
def dnd_commit(self, source, event):
self.dnd_leave(source, event)
x, y = source.where(self.canvas, event)
source.attach(self.canvas, x, y)
def test():
root = tkinter.Tk()
root.geometry("+1+1")
tkinter.Button(command=root.quit, text="Quit").pack()
t1 = Tester(root)
t1.top.geometry("+1+60")
t2 = Tester(root)
t2.top.geometry("+120+60")
t3 = Tester(root)
t3.top.geometry("+240+60")
i1 = Icon("ICON1")
i2 = Icon("ICON2")
i3 = Icon("ICON3")
i1.attach(t1.canvas)
i2.attach(t2.canvas)
i3.attach(t3.canvas)
root.mainloop()
if __name__ == '__main__':
test()
Related
I have a program written in Python, that makes a window where you can draw, using Tkinter. Every time you left-click your mouse, you make a point in your canvas. When you double-click, a polygon is made, filled with the color you chose. I found a way to change the colors from the boxes, when you right-click a box, but the problem is that the selected color is not saved and i cannot make it replace the previous one. Does anyone know how to solve this problem?
import tkinter as tk
from tkinter import colorchooser
class Point():
def __init__(self, canvas, x, y):
self.x = x
self.y = y
canvas.create_oval(x-2, y-2, x+2, y+2, fill='white')
class Poly():
def __init__(self, canvas, board, p_list=[] ):
self.p_list = p_list
self.canvas = canvas
self.board = board
def draw_poly(self):
points = []
for p in self.p_list:
points.extend([p.x, p.y])
points.extend(points[:2])
self.canvas.create_polygon(points, fill=self.board.current_color, outline=self.board.current_color)
def add_point(self, p):
self.p_list.append(p)
if len(self.p_list)>1:
p1 = self.p_list[-1]
p2 = self.p_list[-2]
self.canvas.create_line(p1.x, p1.y, p2.x, p2.y, fill="white", width=2)
class Palette():
def __init__(self, frame, board, colors):
self.colors = colors
self.board = board
self.allColors = []
for color in self.colors:
f = tk.Frame(frame, bg='lightgrey', bd=3)
f.pack(expand=1, fill='both', side='left')
if self.board.current_color == color: f.config(bg='red')
self.allColors.append(f)
l = tk.Label(f, bg=color)
l.pack(expand=1, fill='both', padx=2, pady=2)
l.bind("<1>", self.set_color)
l.bind("<Button-3>", self.do_popup)
def do_popup(self, event):
clsheet = tk.colorchooser.askcolor()
self.current_color = clsheet[1]
def set_color(self, e):
self.board.current_color = e.widget['bg']
self.selected_color(e.widget.master)
def selected_color(self, colorFrame):
for f in self.allColors: f.config(bg = 'lightgrey')
colorFrame.config(bg="red")
class Board():
def __init__(self, root):
self.colors = ['#B4FE98', '#77E4D4', '#F4EEA9', '#F0BB62', '#FF5F7E', "#9A0680"]
self.root = root
self.current_color = self.colors[0]
self.f1 = tk.Frame(self.root)
self.f1.pack(expand=1, fill='both', padx=5)
self.f2 = tk.Frame(self.root)
self.f2.pack(expand=1, fill='both')
self.canvas = tk.Canvas(self.f2, bg="#000D6B", height=550)
self.canvas.pack(expand=1, fill='both', padx=5, pady=5)
self.pallette = Palette(self.f1, self, self.colors )
self.canvas.bind("<1>", self.draw_point)
self.canvas.bind("<Double-Button-1>", self.draw_poly)
self.poly = None
def draw_point(self, evnt):
if self.poly: self.poly.add_point(Point(self.canvas, evnt.x, evnt.y))
else: self.poly = Poly(self.canvas, self, [Point(self.canvas, evnt.x, evnt.y)])
def draw_poly(self, evnt):
if self.poly and len(self.poly.p_list) > 2:
self.poly.add_point(Point(self.canvas, evnt.x, evnt.y))
self.poly.draw_poly()
self.poly = None
else: self.draw_point(evnt)
#main program
root = tk.Tk()
root.title('my program')
root.geometry("600x700")
root.resizable(0,0)
Board(root)
tk.mainloop()
To fix the part where right-clicking a color was not working i changed two things in your script:
You stored your frame widgets and their sub-widget labels in Palette.allColors. I handed over the index of the selected color to the do_popup event by using partial. Then you can simply iterate over all widgets in Palette.allColors and if the index from the event matches the index in the list, you access the children and further the !label key of those and change the background color to the selected color.
I matched Board.current_color and Palette.current_color
Most changes were made in Palette.do_popup(). Might not be the most elegant solution but it looks like its working like you intend. Full code:
import tkinter as tk
from tkinter import colorchooser
from functools import partial
class Point():
def __init__(self, canvas, x, y):
self.x = x
self.y = y
canvas.create_oval(x - 2, y - 2, x + 2, y + 2, fill='white')
class Poly():
def __init__(self, canvas, board, p_list=[]):
self.p_list = p_list
self.canvas = canvas
self.board = board
def draw_poly(self):
points = []
for p in self.p_list:
points.extend([p.x, p.y])
points.extend(points[:2])
self.canvas.create_polygon(points, fill=self.board.current_color, outline=self.board.current_color)
def add_point(self, p):
self.p_list.append(p)
if len(self.p_list) > 1:
p1 = self.p_list[-1]
p2 = self.p_list[-2]
self.canvas.create_line(p1.x, p1.y, p2.x, p2.y, fill="white", width=2)
class Palette():
def __init__(self, frame, board, colors):
self.colors = colors
self.board = board
self.allColors = []
for idx, color in enumerate(self.colors):
f = tk.Frame(frame, bg='lightgrey', bd=3)
f.pack(expand=1, fill='both', side='left')
if self.board.current_color == color: f.config(bg='red')
self.allColors.append(f)
l = tk.Label(f, bg=color)
l.pack(expand=1, fill='both', padx=2, pady=2)
l.bind("<1>", self.set_color)
l.bind("<Button-3>", partial(self.do_popup, idx))
def do_popup(self, idx, event):
clsheet = tk.colorchooser.askcolor()
self.current_color = clsheet[1].upper()
print(f"You chose: {self.current_color}")
self.board.current_color = self.current_color # required?
self.selected_color(event.widget.master)
for frm_idx, frm in enumerate(self.allColors):
if frm_idx == idx:
frm.children["!label"].config(bg=self.current_color)
def set_color(self, e):
self.board.current_color = e.widget['bg']
self.selected_color(e.widget.master)
def selected_color(self, colorFrame):
for f in self.allColors: f.config(bg='lightgrey')
colorFrame.config(bg="red")
class Board():
def __init__(self, root):
self.colors = ['#B4FE98', '#77E4D4', '#F4EEA9', '#F0BB62', '#FF5F7E', "#9A0680"]
self.root = root
self.current_color = self.colors[0]
self.f1 = tk.Frame(self.root)
self.f1.pack(expand=1, fill='both', padx=5)
self.f2 = tk.Frame(self.root)
self.f2.pack(expand=1, fill='both')
self.canvas = tk.Canvas(self.f2, bg="#000D6B", height=550)
self.canvas.pack(expand=1, fill='both', padx=5, pady=5)
self.pallette = Palette(self.f1, self, self.colors)
self.canvas.bind("<1>", self.draw_point)
self.canvas.bind("<Double-Button-1>", self.draw_poly)
self.poly = None
def draw_point(self, evnt):
if self.poly:
self.poly.add_point(Point(self.canvas, evnt.x, evnt.y))
else:
self.poly = Poly(self.canvas, self, [Point(self.canvas, evnt.x, evnt.y)])
def draw_poly(self, evnt):
if self.poly and len(self.poly.p_list) > 2:
self.poly.add_point(Point(self.canvas, evnt.x, evnt.y))
self.poly.draw_poly()
self.poly = None
else:
self.draw_point(evnt)
# main program
root = tk.Tk()
root.title('my program')
root.geometry("600x700")
root.resizable(0, 0)
Board(root)
tk.mainloop()
How to implement closing your own cell editor when it loses focus? Since the built-in wx editors work.
Now my editor closes only if you select another cell in the grid. And, for example, the cell editor (0, 1) closes if you click on the button, and not just when you click on another cell.
My editor and renderer:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.grid
class GridCellColourEditor(wx.grid.GridCellEditor):
def __init__(self):
super().__init__()
def Create(self, parent, id, evtHandler):
self._cp = wx.ColourPickerCtrl(parent, id)
self.SetControl(self._cp)
if evtHandler:
self._cp.PushEventHandler(evtHandler)
def BeginEdit(self, row, col, grid):
self.startValue = grid.GetTable().GetValue(row, col)
self._cp.SetColour(self.startValue)
self._cp.SetFocus()
def EndEdit(self, row, col, grid, oldval):
val = self._cp.GetColour().GetAsString(wx.C2S_HTML_SYNTAX)
if val != oldval:
return val
else:
return None
def ApplyEdit(self, row, col, grid):
val = self._cp.GetColour().GetAsString(wx.C2S_HTML_SYNTAX)
grid.GetTable().SetValue(row, col, val)
def Reset(self):
self._cp.SetColour(self.startValue)
def Clone(self):
return GridCellColourEditor()
class GridCellColourRenderer(wx.grid.GridCellRenderer):
def __init__(self):
super().__init__()
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
if grid.IsEnabled():
bgColour = grid.GetDefaultCellBackgroundColour()
else:
bgColour = grid.GetBackgroundColour()
dc.SetBrush(wx.Brush(bgColour, wx.SOLID))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangle(rect)
colour = grid.GetTable().GetValue(row, col)
x = rect.x + 3
y = rect.y + 3
width = rect.width - 6
height = rect.height - 6
dc.SetBrush(wx.Brush(colour, wx.SOLID))
dc.SetPen(wx.Pen(wx.BLACK))
dc.DrawRoundedRectangle(x, y, width, height, 3)
def GetBestSize(self, grid, attr, dc, row, col):
return attr.GetSize()
def Clone(self):
return GridCellColourRenderer()
class Frame(wx.Frame):
def __init__(self):
super().__init__(None)
vbox = wx.BoxSizer(wx.VERTICAL)
self.grid = wx.grid.Grid(self, size=(100, 50))
vbox.Add(self.grid, flag=wx.EXPAND)
self.grid.CreateGrid(1, 2)
self.grid.SetCellEditor(0, 0, GridCellColourEditor())
self.grid.SetCellRenderer(0, 0, GridCellColourRenderer())
self.grid.SetCellEditor(0, 1, wx.grid.GridCellTextEditor())
self.grid.SetCellRenderer(0, 1, wx.grid.GridCellStringRenderer())
btn = wx.Button(self, -1, 'For kill focus')
vbox.Add(btn, 0, wx.ALL, 10)
self.SetSizer(vbox)
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
Why, if wx.TextCtrl is used as wx.Control, then the cell editor successfully closes when focus is lost. And if you use wx.ColourPickerCtrl, the editor does not close?
It solved my problem
class GridCellColourEditor(wx.grid.GridCellEditor):
def Create(self, parent, id, evtHandler):
self._parent = parent
self._colourDialog = None
self._colourButton = wx.Button(parent, id, "")
self.SetControl(self._colourButton)
newEventHandler = wx.EvtHandler()
if evtHandler:
self._colourButton.PushEventHandler(newEventHandler)
self._colourButton.Bind(wx.EVT_BUTTON, self.OnClick)
def OnClick(self, event):
self._colourButton.SetFocus()
self.ShowColourDialog()
def SetSize(self, rect):
self._colourButton.SetSize(rect.x, rect.y,
rect.width + 2, rect.height + 2,
wx.SIZE_ALLOW_MINUS_ONE)
def Clone(self):
return GridCellColourEditor()
def BeginEdit(self, row, col, grid):
self._grid = grid
self._row = row
self._col = col
self._parent.Bind(wx.EVT_LEFT_DOWN, self.OnParentLeftDown)
self._parent.Bind(wx.EVT_KILL_FOCUS, self.OnParentKillFocus)
self.startValue = grid.GetTable().GetValue(row, col)
self.endValue = self.startValue
self._colourButton.SetBackgroundColour(self.startValue)
def EndEdit(self, row, col, grid, oldval):
self._parent.Unbind(wx.EVT_LEFT_DOWN)
self._parent.Unbind(wx.EVT_KILL_FOCUS)
if self.endValue != self.startValue:
return self.endValue
else:
return None
def ApplyEdit(self, row, col, grid):
val = self.endValue.GetAsString(wx.C2S_HTML_SYNTAX)
grid.GetTable().SetValue(row, col, val)
def Reset(self):
self._colourButton.SetBackgroundColour(self.startValue)
def ShowColourDialog(self):
colourDialog = wx.ColourDialog(self._parent)
self._colourDialog = colourDialog
colourDialog.GetColourData().SetColour(self.startValue)
if colourDialog.ShowModal() == wx.ID_OK:
data = colourDialog.GetColourData()
colour = data.GetColour()
self._colourButton.SetBackgroundColour(colour)
self.endValue = colour
self._parent.SetFocus()
del self._colourDialog
self._colourDialog = None
def OnParentLeftDown(self, event: wx.MouseEvent):
def CheckCellChange():
row = self._grid.GetGridCursorRow()
col = self._grid.GetGridCursorCol()
if self._row == row and self._col == col:
# клик в области сетки, но ячейка не изменилась
self._grid.CloseEditControl()
wx.CallAfter(CheckCellChange)
event.Skip()
def OnParentKillFocus(self, event: wx.FocusEvent):
if self._parent.FindFocus() != self._colourButton:
self._grid.CloseEditControl()
event.Skip()
I have this code which lets the user draw on the canvas and save it as a jpeg file.
As mentioned in this post, I tried to draw in parallel on the canvas and in memory using the PIL so that I can save it as a jpeg instead of postscript. It seemed to be working until I found out that some of the images I saved with PIL are not the same as what was drawn on the canvas.
I assume canvas.create_line and draw.line from the PIL image draw module function similarly and should give similar output.
Below is what went wrong:
For example, when I draw a "T" it seems alright (Left is my drawing, right is the saved image).
But when I draw an "A" , the output image seems a bit weird.
This is my current code:
import Tkinter as tk
import Image,ImageDraw
class ImageGenerator:
def __init__(self,parent,posx,posy,*kwargs):
self.parent = parent
self.posx = posx
self.posy = posy
self.sizex = 200
self.sizey = 200
self.b1 = "up"
self.xold = None
self.yold = None
self.coords= []
self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
self.drawing_area.place(x=self.posx,y=self.posy)
self.drawing_area.bind("<Motion>", self.motion)
self.drawing_area.bind("<ButtonPress-1>", self.b1down)
self.drawing_area.bind("<ButtonRelease-1>", self.b1up)
self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save)
self.button.place(x=self.sizex/7,y=self.sizey+20)
self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear)
self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)
self.image=Image.new("RGB",(200,200),(255,255,255))
self.draw=ImageDraw.Draw(self.image)
def save(self):
print self.coords
self.draw.line(self.coords,(0,128,0),width=3)
filename = "temp.jpg"
self.image.save(filename)
def clear(self):
self.drawing_area.delete("all")
self.coords=[]
def b1down(self,event):
self.b1 = "down"
def b1up(self,event):
self.b1 = "up"
self.xold = None
self.yold = None
def motion(self,event):
if self.b1 == "down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue')
self.coords.append((self.xold,self.yold))
self.xold = event.x
self.yold = event.y
if __name__ == "__main__":
root=tk.Tk()
root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10))
root.config(bg='white')
ImageGenerator(root,10,10)
root.mainloop()
Where did I go wrong and what should I do to save the exact same picture that is drawn on the canvas as a jpeg image?
I managed to solved my problem.In fact i was just being dumb. Even though canvas.create_line and draw.line have the similar function, i didn't use the same exact data to draw out both images. after making changes, this is my working code.
import Tkinter as tk
import Image,ImageDraw
class ImageGenerator:
def __init__(self,parent,posx,posy,*kwargs):
self.parent = parent
self.posx = posx
self.posy = posy
self.sizex = 200
self.sizey = 200
self.b1 = "up"
self.xold = None
self.yold = None
self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
self.drawing_area.place(x=self.posx,y=self.posy)
self.drawing_area.bind("<Motion>", self.motion)
self.drawing_area.bind("<ButtonPress-1>", self.b1down)
self.drawing_area.bind("<ButtonRelease-1>", self.b1up)
self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save)
self.button.place(x=self.sizex/7,y=self.sizey+20)
self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear)
self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)
self.image=Image.new("RGB",(200,200),(255,255,255))
self.draw=ImageDraw.Draw(self.image)
def save(self):
filename = "temp.jpg"
self.image.save(filename)
def clear(self):
self.drawing_area.delete("all")
self.image=Image.new("RGB",(200,200),(255,255,255))
self.draw=ImageDraw.Draw(self.image)
def b1down(self,event):
self.b1 = "down"
def b1up(self,event):
self.b1 = "up"
self.xold = None
self.yold = None
def motion(self,event):
if self.b1 == "down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue')
self.draw.line(((self.xold,self.yold),(event.x,event.y)),(0,128,0),width=3)
self.xold = event.x
self.yold = event.y
if __name__ == "__main__":
root=tk.Tk()
root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10))
root.config(bg='white')
ImageGenerator(root,10,10)
root.mainloop()
make a litte change about mouse event
"""
画板,鼠标移动速度太快可以造成不连续的点
tkinter 8.6.11
https://stackoverflow.com/questions/17915440/python-tkinter-save-canvas-as-image-using-pil
"""
import tkinter as tk
from PIL import Image, ImageDraw
class ImageGenerator:
def __init__(self, parent, posx, posy, *kwargs):
self.parent = parent
self.posx = posx
self.posy = posy
self.sizex = 280
self.sizey = 280
self.penColor = "white" # 画笔的颜色 (255, 255, 255)
self.backColor = "black" # 画布背景色 (0, 0, 0)
self.penWidth = 10 # 笔刷的宽度
self.drawing_area = tk.Canvas(
self.parent, width=self.sizex, height=self.sizey, bg=self.backColor
)
self.drawing_area.place(x=self.posx, y=self.posy)
self.drawing_area.bind("<B1-Motion>", self.motion)
self.button = tk.Button(
self.parent, text="Done", width=10, bg="white", command=self.save
)
self.button.place(x=self.sizex / 7, y=self.sizey + 20)
self.button1 = tk.Button(
self.parent, text="Clear", width=10, bg="white", command=self.clear
)
self.button1.place(x=(self.sizex / 7) + 80, y=self.sizey + 20)
self.image = Image.new("RGB", (self.sizex, self.sizey), (0, 0, 0))
self.draw = ImageDraw.Draw(self.image)
def save(self):
filename = "temp.jpg"
self.image.save(filename)
def clear(self):
"""将画板和image清空"""
self.drawing_area.delete("all")
self.image = Image.new("RGB", (self.sizex, self.sizey), (0, 0, 0))
self.draw = ImageDraw.Draw(self.image)
def motion(self, event):
"""在画板和image上同时绘制"""
self.drawing_area.create_oval(
event.x,
event.y,
event.x + self.penWidth,
event.y + self.penWidth,
fill=self.penColor,
outline=self.penColor,
) # 在画布上画
self.draw.ellipse(
(
(event.x, event.y),
(event.x + self.penWidth, event.y + self.penWidth),
),
fill=self.penColor,
outline=self.penColor,
width=self.penWidth,
) # 在生成的图上画,point、line都太细
if __name__ == "__main__":
root = tk.Tk()
root.wm_geometry("%dx%d+%d+%d" % (300, 350, 10, 10))
root.config(bg="white")
ImageGenerator(root, 10, 10)
root.mainloop()
I can track where the user clicks and where they release but I want to track distance traveled.
from Tkinter import *
root = Tk()
class DragCursor():
def __init__(self, location):
self.label = location
location.bind('<ButtonPress-1>', self.StartMove)
location.bind('<ButtonRelease-1>', self.StopMove)
def StartMove(self, event):
startx = event.x
starty = event.y
print [startx, starty]
def StopMove(self, event):
self.StartMove
stopx = event.x
stopy = event.y
print [stopx, stopy]
location = Canvas(root, width = 300, height = 300)
DragCursor(location)
location.pack()
root.mainloop()
You just need to use the distance formula for determining the distance between two points in an xy-plane,
Also, you need to include some kind of instance variable that will save the coordinates for the start and end points so that you can compute it after the mouse release.
This is pretty much your code just with a new distancetraveled function that is printed at the end of StopMove using self.positions.
from Tkinter import *
root = Tk()
class DragCursor():
def __init__(self, location):
self.label = location
location.bind('<ButtonPress-1>', self.StartMove)
location.bind('<ButtonRelease-1>', self.StopMove)
self.positions = {}
def StartMove(self, event):
startx = event.x
starty = event.y
self.positions['start'] = (startx, starty)
def StopMove(self, event):
stopx = event.x
stopy = event.y
self.positions['stop'] = (stopx, stopy)
print self.distancetraveled()
def distancetraveled(self):
x1 = self.positions['start'][0]
x2 = self.positions['stop'][0]
y1 = self.positions['start'][1]
y2 = self.positions['stop'][1]
return ((x2-x1)**2 + (y2-y1)**2)**0.5
location = Canvas(root, width = 300, height = 300)
DragCursor(location)
location.pack()
root.mainloop()
Can anyone tell me why my 'addLine' method is failing to call when the self.rec button is clicked?
from tkinter import *
from tkinter import ttk
root = Tk()
class Paint:
def __init__(self, parent):
self.parent = parent
self.whiteBoard = Canvas(self.parent)
self.whiteBoard.grid(column=0, row=0, sticky=(N,W,E,S))
self.lastx = 0
self.lasty = 0
self.rec = self.whiteBoard.create_rectangle((10, 10, 30, 30), fill="red")
self.whiteBoard.tag_bind(self.rec, "<Button-1>", self.getClick)
def xy(self, event):
self.lastx, self.lasty = event.x, event.y
print (event.x, " is the x coordinate")
print (event.y, " is the y coordinate")
def addLine(self, event):
canvas.create_line((lastx, lasty, event.x, event.y))
self.lastx, self.lasty = event.x, event.y
def getClick(self, event):
self.whiteBoard.bind("<Button-1>", self.xy)
self.whiteBoard.bind("B1-Motion>", self.addLine)
white = Paint(root)
root.mainloop()
This is all part of an attempt to make a MS paint clone using Tkinter.
First, you are binding to B1-Motion> (note the missing <). But, more importantly, don't do bindings like those. It looks like the getClick method is actually "select the line tool". Then, add the bindings <Button-1> and <B1-Motion> to the canvas itself. When the callbacks are called, you do the actions according to the selected tool.
Here is rough sketch that follows this suggestion (with a RECTANGLE tool as a bonus):
import tkinter
# TOOLS
LINE, RECTANGLE = list(range(2))
class Paint:
def __init__(self, canvas):
self.canvas = canvas
self._tool, self._obj = None, None
self.lastx, self.lasty = None, None
self.canvas.bind('<Button-1>', self.update_xy)
self.canvas.bind('<B1-Motion>', self.draw)
def draw(self, event):
if self._tool is None or self._obj is None:
return
x, y = self.lastx, self.lasty
if self._tool in (LINE, RECTANGLE):
self.canvas.coords(self._obj, (x, y, event.x, event.y))
def update_xy(self, event):
if self._tool is None:
return
x, y = event.x, event.y
if self._tool == LINE:
self._obj = self.canvas.create_line((x, y, x, y))
elif self._tool == RECTANGLE:
self._obj = self.canvas.create_rectangle((x, y, x, y))
self.lastx, self.lasty = x, y
def select_tool(self, tool):
print('Tool', tool)
self._tool = tool
class Tool:
def __init__(self, whiteboard, parent=None):
self.whiteboard = whiteboard
frame = tkinter.Frame(parent)
self._curr_tool = None
for i, (text, t) in enumerate((('L', LINE), ('R', RECTANGLE))):
lbl = tkinter.Label(frame, text=text, width=2, relief='raised')
lbl._tool = t
lbl.bind('<Button-1>', self.update_tool)
lbl.pack(padx=6, pady=6*(i % 2))
frame.pack(side='left', fill='y', expand=True, pady=6)
def update_tool(self, event):
lbl = event.widget
if self._curr_tool:
self._curr_tool['relief'] = 'raised'
lbl['relief'] = 'sunken'
self._curr_tool = lbl
self.whiteboard.select_tool(lbl._tool)
root = tkinter.Tk()
canvas = tkinter.Canvas(highlightbackground='black')
whiteboard = Paint(canvas)
tool = Tool(whiteboard)
canvas.pack(fill='both', expand=True, padx=6, pady=6)
root.mainloop()