我正在尝试创建一个使用tkinter显示图像的程序。到目前为止,我编写的代码可以完美显示图像,但是直到tkinter窗口关闭后,程序才能继续执行。我想知道是否有任何函数代替.mainloop()来保持程序在tkinter窗口旁边运行?任何帮助,不胜感激
window = Tk()
canvas1 = Canvas(window,height=600,width=600)
file = PhotoImage(file = 'GoogleDrive.png')
image = canvas1.create_image(300, 300, anchor = CENTER, image = file)
canvas1.pack()
canvas1.mainloop()
print ('\nA: Google Drive')
time.sleep(1)
print ('B: Google Slides')
time.sleep(1)
print ('C: Google Photos')
最佳答案
mainloop
必须是代码的最后逻辑行。对于您的情况Tk
,还应在window
对象上调用mainloop。
为了使代码运行,您需要将print
和sleep
语句移到mainloop
之前,或者将它们移至一个函数,然后在主循环之前调用该函数。
还值得注意的是,在Tkinter中,可以将after
用作计时而不是sleep
。