如何在 Python Tkinter 中一段時間後隱藏小元件?
Tkinter 是用於開發基於 GUI 的應用程式的標準 Python 庫。我們可以使用 Tkinter 庫建立遊戲、工具和其他應用程式。為了開發基於 GUI 的應用程式,Tkinter 提供小元件。
有時,可能需要隱藏小元件一段時間。這可以透過使用 pack_forget() 方法來實現。當我們使用各種方法在視窗中打包小元件時,我們必須使用相同的方法來隱藏該小元件。
示例
# Import the required libraries from tkinter import * from PIL import Image, ImageTk # Create an instance of tkinter frame or window win=Tk() # Set the size of the tkinter window win.geometry("700x350") # Create a canvas widget canvas=Canvas(win, width=400, height=300) canvas.pack() # Add an image in the canvas widget img=ImageTk.PhotoImage(file="baseball.png") canvas.create_image(100, 150,image=img) # Hide the image from the canvas after sometime canvas.after(3000, canvas.pack_forget) win.mainloop()
輸出
執行給定的程式碼將在 Canvas 小元件中顯示一張影像,該影像將在一段時間後消失。
廣告