Informatik10/tk_canvas/my_canvas.py

80 lines
2.5 KiB
Python
Raw Normal View History

import tkinter as tk
import tkinter.ttk as ttk
class Controller(object):
def __init__(self, app):
self.app = app
self.selected = None
self.selected_start = None
self.selected_end = None
def run_process(self):
print("Run")
self.app.main_canvas.bind('<1>', self.make_circle)
self.app.main_canvas.bind('<3>', self.select_item)
self.app.main_canvas.bind('<Shift-1>', self.select_for_line)
return 0
def make_circle(self, event):
x = event.x
y = event.y
r = 20
self.app.main_canvas.create_oval(x-r, y-r, x+r, y+r, fill="white", outline="black", tag="circle")
def select_item(self, event):
self.app.main_canvas.bind('<Motion>', self.move_item)
self.app.main_canvas.bind('<1>', self.deselect_item)
self.app.main_canvas.addtag_withtag('selected', tk.CURRENT)
def deselect_item(self, event):
self.app.main_canvas.dtag('selected')
self.app.main_canvas.unbind('<Motion>')
self.app.main_canvas.bind('<1>', self.make_circle)
def move_item(self, event):
x = event.x
y = event.y
r = 20
self.app.main_canvas.coords("selected",x-r, y-r, x+r, y+r)
def select_for_line(self, event):
self.app.main_canvas.addtag_withtag("sel_line", tk.CURRENT)
start_coords = self.app.main_canvas.coords("sel_line")
self.x0 = (start_coords[0] + start_coords[2]) // 2
self.y0 = (start_coords[1] + start_coords[3]) // 2
x = event.x
y = event.y
self.app.main_canvas.create_line(self.x0,self.y0,x,y, tag="newline")
self.app.main_canvas.bind('<Motion>', self.move_line)
def move_line(self, event):
x = event.x
y = event.y
self.app.main_canvas.coords("newline",self.x0,self.y0,x,y)
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.con = Controller(self)
self.create_control_variables()
self.create_widgets()
def create_control_variables(self):
pass
def create_widgets(self):
self.main_canvas = tk.Canvas(master=self, width=500, height=400, background='gray75')
self.main_canvas.grid(row=0, column=0)
self.b = ttk.Button(master=self, text="Start", command=self.con.run_process)
self.b.grid(row=1, column=0)
# --------------------------
# Execution
# --------------------------
if __name__ == "__main__":
app = Application()
app.mainloop()