Last active
April 15, 2022 03:32
-
-
Save shiracamus/31d911fe8dbb940dded3831ea349fc2e to your computer and use it in GitHub Desktop.
Python Tkinterで点滅させる方法 https://qiita.com/Ovis/questions/11f541611185d70c8925#answer-4c75d28128f6849cb266
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
import tkinter as tk | |
import math | |
import pyautogui | |
info = { | |
'position': (-1, -1), | |
'state': tk.NORMAL, | |
} | |
def triangle(x, y, edge, angle): | |
ok = edge / 2 / (3**0.5) | |
r = math.radians(angle) | |
sin_r, cos_r = math.sin(r), math.cos(r) | |
x1, y1 = -edge/2, ok-100 | |
x2, y2 = edge/2, ok-100 | |
x3, y3 = 0, -120 | |
def rotate(px, py): | |
return ((px * cos_r - py * sin_r) + x, | |
(px * sin_r + py * cos_r) + y) | |
canvas.create_polygon( | |
*rotate(x1, y1), | |
*rotate(x2, y2), | |
*rotate(x3, y3), | |
fill="#ff0000", | |
state=info['state'], | |
tags="triangle") | |
def stalker(): | |
"""追従""" | |
position = pyautogui.position() | |
if info['position'] != position: | |
x, y = info['position'] = position | |
canvas.delete("all") | |
y -= 50 | |
edge = 100 | |
triangle(x, y, edge, 0) | |
triangle(x, y, edge, 90) | |
triangle(x, y, edge, 180) | |
triangle(x, y, edge, 270) | |
canvas.after(50, stalker) | |
def blink(): | |
"""点滅""" | |
info['state'] = tk.HIDDEN if info['state'] == tk.NORMAL else tk.NORMAL | |
canvas.itemconfigure('triangle', state=info['state']) | |
canvas.after(500, blink) # 500ミリ秒間隔、1秒間に1周期、1ヘルツ | |
root = tk.Tk() | |
root.geometry("1920x1080") | |
canvas = tk.Canvas(root, background="#fff", width=1920, height=1080) | |
canvas.pack() | |
stalker() | |
canvas.after(300, blink) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment