【问题标题】:How do you animate the creation of a widget in tkinter?你如何在 tkinter 中创建一个小部件的动画?
【发布时间】:2019-12-01 20:48:20
【问题描述】:

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

这是我目前的代码:

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 中可以做这样的事情吗?

【问题讨论】:

    标签: python python-3.x animation tkinter


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 2015-02-06
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2010-11-26
      • 2011-02-22
      • 1970-01-01
      相关资源
      最近更新 更多