Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/python-moving-objects/main.py
5925 views
1
from tkinter import *
2
from tkinter import messagebox
3
4
5
def on_closing():
6
if messagebox.askokcancel("Выход из приложения", "Хотите выйти из приложения?"):
7
tk.destroy()
8
9
10
tk = Tk()
11
tk.protocol("WM_DELETE_WINDOW", on_closing)
12
tk.title("Мое приложение")
13
tk.resizable(0, 0)
14
tk.wm_attributes("-topmost", 1)
15
16
canvas = Canvas(tk, width=600, height=600, highlightthickness=0)
17
canvas.pack()
18
19
canvas1 = Canvas(tk, width=100, height=100)
20
canvas1.place(x=100, y=100, anchor=CENTER)
21
canvas1.create_rectangle(0, 0, 100, 100, fill="red")
22
23
canvas2 = Canvas(tk, width=100, height=100)
24
canvas2.place(x=300, y=300, anchor=CENTER)
25
canvas2.create_oval(5, 5, 95, 95, fill="red")
26
canvas2.create_oval(10, 10, 90, 90, fill="yellow")
27
28
canvas3 = Canvas(tk, width=100, height=100)
29
canvas3.place(x=300, y=100, anchor=CENTER)
30
canvas3.create_rectangle(0, 0, 100, 100, fill="green")
31
32
canvas4 = Canvas(tk, width=300, height=100)
33
canvas4.place(x=200, y=500, anchor=CENTER)
34
canvas4.create_oval(5, 5, 295, 95, fill="blue")
35
canvas4.create_oval(10, 10, 290, 90, fill="yellow")
36
37
38
def drag(event):
39
#print(event.x_root, event.y_root)
40
mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
41
mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
42
#print(mouse_x, mouse_y)
43
event.widget.place(x=mouse_x, y=mouse_y, anchor=CENTER)
44
45
46
canvas1.bind("<B1-Motion>", drag)
47
canvas2.bind("<B1-Motion>", drag)
48
canvas3.bind("<B1-Motion>", drag)
49
canvas4.bind("<B1-Motion>", drag)
50
51
tk.mainloop()
52
53