Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- import time
- import random
- tk = Tk()
- app_running = True
- size_canvas_x = 700
- size_canvas_y = 500
- menu_x = 250
- menu_y = 50
- canvas_elements = []
- def click_canvas(event):
- mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
- mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
- if 0 <= mouse_x <= size_canvas_x and 0 <= mouse_y <= size_canvas_y:
- x = mouse_x
- y = mouse_y
- rgb_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- canvas_elements.append(canvas.create_rectangle(x, y, x + 50, y + 50, fill=_from_rgb(rgb_color)))
- def _from_rgb(rgb):
- """translates an rgb tuple of int to a tkinter friendly color code
- """
- return "#%02x%02x%02x" % rgb
- def test_canvas_clear():
- if canvas_elements:
- cid = canvas_elements.pop()
- canvas.delete(cid)
- def test_canvas():
- x = random.randint(0, size_canvas_x)
- y = random.randint(0, size_canvas_y)
- rgb_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- canvas_elements.append(canvas.create_rectangle(x, y, x+50, y+50, fill=_from_rgb(rgb_color)))
- def on_closing():
- global app_running
- if messagebox.askokcancel("Выход из игры", "Хотите выйти из игры?"):
- app_running = False
- tk.destroy()
- tk.protocol("WM_DELETE_WINDOW", on_closing)
- tk.title("Test")
- tk.resizable(0, 0)
- tk.wm_attributes("-topmost", 1)
- canvas = Canvas(tk, width=size_canvas_x + menu_x, height=size_canvas_y + menu_y, bd=0,
- highlightthickness=0)
- canvas.create_rectangle(0, 0, size_canvas_x, size_canvas_y, fill="white")
- canvas.pack()
- tk.update()
- button1 = Button(text="Test", command=test_canvas)
- button1.place(x=size_canvas_x+10, y=20, width=230, height=50)
- button2 = Button(text="Clear", command=test_canvas_clear)
- button2.place(x=size_canvas_x+10, y=90, width=230, height=50)
- canvas.bind_all("<Button-1>", click_canvas) # ЛКМ
- canvas.bind_all("<Button-3>", click_canvas) # ПКМ
- while app_running:
- if app_running:
- tk.update_idletasks()
- tk.update()
- time.sleep(0.005)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement