如何在tkinter中创建小部件的动画?

时间:2019-12-01 20:48:20

标签: python python-3.x animation tkinter

我有一个框架,当按下按钮时该框架会消失。我希望框架在出现时具有动画效果,就像从按钮上展开一样。

这是我到目前为止的代码:

button_state = False #checks state of button
def click():
    button_state ^= True #switches between True and False
    if button_state == True:
        frame.place(x=50, y=30, width=10, height=10)
    else:
        frame.place_forget() #makes frame disappear

frame = Frame(window, bg='lightblue')

button = Button(window, text="menu", command=click)
button.place(x=50, y=50, anchor=CENTER)

有可能在tkinter中做这样的事情吗?

1 个答案:

答案 0 :(得分:1)

您可以使用for循环来调整框架的大小,以模拟扩展效果:

def click():
  if frame.place_info():
    frame.place_forget()
  else:
    # show the frame below the button
    x, y = button.winfo_x(), button.winfo_y()+button.winfo_height()
    # assume the final size of the frame is 100x100
    for step in range(1, 11):
      frame.place(x=x, y=y, width=step*10, height=step*10)
      frame.update_idletasks() # update the frame
      frame.after(10) # sleep for a very short period