如何隱藏、恢復和刪除 Tkinter 控制元件
在本教程中,我們將介紹如何通過單擊按鈕來隱藏,恢復 Tkinter 控制元件。最後,我們還將向你展示如何刪除或殺死現有的 Tkinter 控制元件。
隱藏和恢復 Tkinter 控制元件
pack_forget()
隱藏 Tkinter 控制元件的方法
與 Tkinter 控制元件中的 pack
方法相反,我們可以通過呼叫 pack_forget()
method 使控制元件不可見來隱藏 Tkinter 控制元件。
try:
import Tkinter as tk
except:
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.label=tk.Label(self.root,
text = "Label")
self.buttonForget = tk.Button(self.root,
text = 'Click to hide Label',
command=lambda: self.label.pack_forget())
self.buttonRecover = tk.Button(self.root,
text = 'Click to show Label',
command=lambda: self.label.pack())
self.buttonForget.pack()
self.buttonRecover.pack()
self.label.pack(side="bottom")
self.root.mainloop()
def quit(self):
self.root.destroy()
app = Test()
self.buttonForget = tk.Button(self.root,
text = 'Click to hide Label',
command=lambda: self.label.pack_forget())
pack_forget()
繫結 buttonForget
到按鈕命令。
單擊按鈕後,它將隱藏標籤控制元件,但是標籤本身仍然存在,只是變得不可見。
我們需要再次呼叫 pack()
方法來將此控制元件變得可見,或者換句話說,將其恢復。
grid_forget()
隱藏 Tkinter 控制元件的方法如果使用 grid
佈局
如果將控制元件放置在 grid
佈局管理器中,則應使用 grid_forget()
方法使 Tkinter 控制元件不可見。
try:
import Tkinter as tk
except:
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.label=tk.Label(self.root,
text = "Label")
self.buttonForget = tk.Button(self.root,
text = 'Click to hide Label',
command=lambda: self.label.grid_forget())
self.buttonRecover = tk.Button(self.root,
text = 'Click to show Label',
command=lambda: self.label.grid())
self.buttonForget.grid(column=0, row=0, padx=10, pady=10)
self.buttonRecover.grid(column=0, row=1, padx=10, pady=10)
self.label.grid(column=0, row=2, padx=10, pady=10)
self.root.mainloop()
def quit(self):
self.root.destroy()
app = Test()
self.buttonForget = tk.Button(self.root,
text = 'Click to hide Label',
command=lambda: self.label.grid_forget())
在這裡,我們將 grid_forget
方法繫結到按鈕的命令。
你可能會注意到,在再次呼叫 grid
方法之後,標籤的位置在隱藏之前是不一樣的。
如果我們打算將標籤恢復到其原始位置,則應該使用 grid_remove
方法。
grid_remove()
隱藏 Tkinter 控制元件的方法,如果使用 grid
佈局
grid_remove()
隱藏控制元件,但它會記住諸如 column
和 row
的網格選項。
try:
import Tkinter as tk
except:
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.label=tk.Label(self.root,
text = "Label")
self.buttonForget = tk.Button(self.root,
text = 'Click to hide Label',
command=lambda: self.label.grid_remove())
self.buttonRecover = tk.Button(self.root,
text = 'Click to show Label',
command=lambda: self.label.grid())
self.buttonForget.grid(column=0, row=0, padx=10, pady=10)
self.buttonRecover.grid(column=0, row=1, padx=10, pady=10)
self.label.grid(column=0, row=2, padx=10, pady=20)
self.root.mainloop()
def quit(self):
self.root.destroy()
app = Test()
通過單擊一個按鈕永久刪除 Tkinter 控制元件
我們將展示如何通過呼叫 destroy
方法來永久刪除 Tkinter 控制元件。
try:
import Tkinter as tk
except:
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.label=tk.Label(self.root,
text = "Label")
self.buttonForget = tk.Button(self.root,
text = 'Click to hide Label',
command=lambda: self.label.destroy())
self.buttonRecover = tk.Button(self.root,
text = 'Click to show Label',
command=lambda: self.label.pack())
self.buttonForget.pack()
self.buttonRecover.pack()
self.label.pack(side="bottom")
self.root.mainloop()
def quit(self):
self.root.destroy()
app = Test()
單擊按鈕後,按鈕將被刪除。
你可以嘗試單擊 buttonRecover
按鈕,但會收到如下的 _tkinter.TclError
錯誤通知,
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\WinPython\WPy-3661\python-3.6.6.amd64\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\jinku\OneDrive\Bureaublad\test.py", line 17, in <lambda>
command=lambda: self.label.pack())
File "D:\WinPython\WPy-3661\python-3.6.6.amd64\lib\tkinter\__init__.py", line 2140, in pack_configure
+ self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!label"
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn