Created
April 8, 2021 18:17
-
-
Save rodrigo-x/62d7cdcc45a6fb72d221a87a20855ae0 to your computer and use it in GitHub Desktop.
tkinter + canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import tkinter as tk | |
root = tk.Tk() | |
# Aqui é aquele velho X e Y ou Largura e Altura respectivamente | |
canvas_width = 500 | |
canvas_height = 150 | |
# Função que pega os eventos do mouse | |
def desenhar(event): | |
# Cor dos pontos no Canvas | |
cor_dos_pontos = '#BC243C' | |
# X e Y = event.x e event.y | |
x1, y1 = (event.x - 1), (event.y - 1) | |
x2, y2 = (event.x + 1), (event.y + 1) | |
# Usando o create_arc pra criar um arco com os eVentos X e Y | |
canvas.create_arc(x1, y1, x2, y2, fill = cor_dos_pontos) | |
# Invocando o widget Canvas | |
canvas = tk.Canvas(root, width = canvas_width, | |
height = canvas_height) | |
# Lembrando de novo, é possível utilizar o pack(), o grid() ou o place() | |
canvas.pack(expand = tk.YES, fill = tk.BOTH) | |
# Colocando o bind no ponteiro do mouse e passando os eventos pra função desenhar | |
canvas.bind('<B1-Motion>', desenhar) | |
# Label do Canvas | |
label = tk.Label(root, text = 'Passe o mouse pela superfície da Window') | |
label.place(x = 10, y = 10) | |
label.config(fg = 'black') | |
# Aquela Factory de sempre | |
root.title('Canvas - Pintando com o Canvas Arc') | |
root.geometry('400x350') | |
root.resizable(False, False) | |
root.eval('tk::PlaceWindow . center') | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment