Skip to content

Instantly share code, notes, and snippets.

@villares
Last active October 4, 2021 15:17
Show Gist options
  • Save villares/b57afde9a6fd1c9c74a3567e963e0f8d to your computer and use it in GitHub Desktop.
Save villares/b57afde9a6fd1c9c74a3567e963e0f8d to your computer and use it in GitHub Desktop.
Exemplo mínimo de desenhar linhas com o mouse no Tkinter (vide mais referências de Tkinter abaixo)
# Baseado em https://stackoverflow.com/a/40877887/19771
from tkinter import *
current = None
def DrawGrid(drawing_area, line_distance):
for x in range(line_distance,600,line_distance):
drawing_area.create_line(x, 0, x, 600, fill="#d3d3d3")
for y in range(line_distance,600,line_distance):
drawing_area.create_line(0, y, 600, y, fill="#d3d3d3")
def main():
root = Tk()
drawing_area = Canvas(root, width=600, height=600, bg='white')
drawing_area.pack()
DrawGrid(drawing_area, 10)
drawing_area.bind("<ButtonPress-3>", reset) # botão da direita
drawing_area.bind("<Motion>", motion)
drawing_area.bind("<ButtonPress-1>", Mousedown) # botão da esquerda
root.mainloop()
def reset(event):
global current
current = None
def Mousedown(event):
global current
event.widget.focus_set() # so escape key will work
if current is None:
# the new line starts where the user clicked
x0 = event.x
y0 = event.y
else:
# the new line starts at the end of the previously
# drawn line
coords = event.widget.coords(current)
x0 = coords[2]
y0 = coords[3]
# create the new line
current = event.widget.create_line(x0, y0, event.x, event.y)
def motion(event):
if current:
# modify the current line by changing the end coordinates
# to be the current mouse position
coords = event.widget.coords(current)
coords[2] = event.x
coords[3] = event.y
event.widget.coords(current, *coords)
main()

Eventos acionados por teclas e outros "bindings":

Janelas de mensagem e painel de abrir arquivos ("dialogs"):

import tkinter as tk
from tkinter.colorchooser import askcolor                  
from tkinter import filedialog as fd 

def picker_callback():
    result = askcolor(
        color="#6A9662", 
        title="Color Picker"
    ) 
    print(result)

def open_callback():
    name= fd.askopenfilename(title="Escolha um arquivo")
    print(name)

def quit_procedure():
    print('Tratar encerramento aqui')
    root.destroy() # sem isto agora não fecha a janela!

root = tk.Tk()

root.wm_protocol ("WM_DELETE_WINDOW", quit_procedure) # captura o fechamento com X da janela

tk.Button(
    root, 
    text='Choose Color', 
    fg="darkgreen", 
    command=picker_callback
).pack(side=tk.LEFT, padx=10)

tk.Button(
    text='File Open', 
    command=open_callback
).pack(fill=tk.X)

tk.Button(
    text='Quit', 
    command=quit_procedure,  # no exemplo original era command=root.quit,
    fg="red"
).pack(side=tk.LEFT, padx=10)

tk.mainloop()

# about quit/destroy:
# https://bytes.com/topic/python/answers/390009-quitting-tkinter-application-confirmation
# -> https://stackoverflow.com/questions/4643007/intercept-tkinter-exit-command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment