Неуклюжая / медленная функциональность перетаскивания на моем проекте аналоговых часов - PullRequest
2 голосов
/ 20 февраля 2020

Я только что закончил свой первый Python проект, аналоговые часы. Мне удалось достичь состояния, в котором я реализовал все, что имел в виду, и устранил последующие проблемы. Последнее, что я добавил, это возможность перетаскивания моих часов, и пока они работают, они кажутся медленными или, по крайней мере, просто плавными. Прямо сейчас мне не хватает знаний и опыта, чтобы понять, почему это происходит, и не говоря уже о том, чтобы это исправить. Любая помощь или идеи будут оценены, спасибо заранее. :)

Вот мой код:

# Imported tkinter for its methods
import tkinter as tk

# Imported turtle for drawing the clock's hands
import turtle

# Imported time to handle setting and updating the time
import time


# Declared and set the color used for transparency
transparent_clr = '#FB00FF'

# Setup a borderless window with transparent background
# and always on top flag
root = tk.Tk()
root.overrideredirect(True)
root.wm_attributes('-topmost', 1)
root.deiconify()
root.attributes('-transparentcolor', transparent_clr)

# Setup the clock's face using an image
Clock_bg = tk.PhotoImage(file='Clock_bg.png')
canvas = tk.Canvas(width=300, height=300, highlightthickness=0)
screen = turtle.TurtleScreen(canvas)
canvas.create_image(0, 0, image=Clock_bg)
canvas.pack()

screen.tracer(0)
screen.bgcolor(transparent_clr)

# Configure the pen used for the clock's hands
draw = turtle.RawTurtle(screen)
draw.hideturtle()
draw.speed(0)
draw.pensize(3)

# Retain Windows TaskBar visibility and function such as exiting
# the app
wn = tk.Toplevel(root)
wn.iconify()
wn.iconbitmap('Clock_icon.ico')
wn.attributes('-alpha', 0.0)


def wn_destroy():
    wn.protocol('WM_DELETE_WINDOW', exit_func)


def exit_func():
    root.destroy()


wn_destroy()

# Make the clock draggable


def draggable():
    root._offsetx = 0
    root._offsety = 0
    root.bind('<Button-1>', winclick)
    root.bind('<B1-Motion>', windrag)


def windrag(event):
    x = root.winfo_pointerx() - root._offsetx
    y = root.winfo_pointery() - root._offsety
    root.geometry('+{x}+{y}'.format(x=x, y=y))


def winclick(event):
    root._offsetx = event.x
    root._offsety = event.y


draggable()

# Draw the clock and its hands


def draw_clock(h, m, s, draw):

    # Draw the hours hand
    draw.penup()
    draw.goto(0, 0)
    draw.color('black')
    draw.setheading(90)
    angle = (h / 12) * 360 + (m / 60) * 30
    draw.rt(angle)
    draw.pendown()
    draw.fd(70)

    # Draw the minutes hand
    draw.penup()
    draw.goto(0, 0)
    draw.color('black')
    draw.setheading(90)
    angle = (m / 60) * 360 + (s / 60) * 6
    draw.rt(angle)
    draw.pendown()
    draw.fd(100)

    # Draw the seconds hand
    draw.penup()
    draw.goto(0, 0)
    draw.color('red')
    draw.setheading(90)
    angle = (s / 60) * 360
    draw.rt(angle)
    draw.pendown()
    draw.fd(60)


# Update the time in real time
while True:
    # Declared and set the hour, minutes and seconds
    h = int(time.strftime('%I'))
    m = int(time.strftime('%M'))
    s = int(time.strftime('%S'))

    draw_clock(h, m, s, draw)
    screen.update()

    time.sleep(1)

    draw.clear()

1 Ответ

4 голосов
/ 20 февраля 2020

"Вы используете свой собственный mainl oop с time.sleep (1), поэтому root .geometry (вызывается только каждую секунду. –Stovfl"

Спасибо за миллиард, stovf1!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...