【问题标题】:Python 2.7 time.sleep() with Canvas Tkinter (OS X) [duplicate]带有 Canvas Tkinter(OS X)的 Python 2.7 time.sleep() [重复]
【发布时间】:2016-08-17 01:37:26
【问题描述】:

我正在使用 Tkinter 在 Python 2.7 中制作程序,我想创建 3 秒的介绍。介绍只是应该显示 3 秒然后被删除的画布图像。问题是我的程序启动了 3 秒,然后代码就完成了,所以没有介绍。我读到这是因为输出缓冲。我不知道如何禁用它,因为每个人都在谈论 time.sleep 和打印功能。这是我的代码:

 root = Tk()
 root.resizable(0,0)
 root.geometry('800x600+200+200')

 #canvas UI
 w = Tkinter.Canvas(root, bd=0, height=600, width=800)

 def intro():
     w.pack()
     intro = Tkinter.PhotoImage(file=r'intro.ppm')
     root.intro = intro
     w.create_image((0,0), image=intro, anchor='nw', tags=("intro"))
     time.sleep(3)
     w.delete("intro")

 intro()

 w.pack()

 root.mainloop ()

【问题讨论】:

    标签: python macos python-2.7 tkinter tkinter-canvas


    【解决方案1】:

    你不能在 Tkinter 中使用time.sleep。当您希望程序暂停时,请使用after

    def intro():
        w.pack()
        intro = Tkinter.PhotoImage(file=r'intro.ppm')
        root.intro = intro
        w.create_image((0,0), image=intro, anchor='nw', tags=("intro"))
        w.after(3000, w.delete, "intro")
    

    【讨论】:

    • 我想你的意思是w.after(3000, w.delete, "intro")
    • @TadhgMcDonald-Jensen 谢谢,已编辑答案。
    猜你喜欢
    • 2018-01-19
    • 2014-08-30
    • 1970-01-01
    • 2011-12-28
    • 2016-04-19
    • 2016-11-29
    • 2016-06-25
    • 2014-07-04
    • 2023-03-10
    相关资源
    最近更新 更多