只是在尋找一個例子,我知道它可以使用按鈕,但我想使用 Checkbutton 的不同狀態(onvalue 和 offvalue)來顯示和隱藏標簽。
uj5u.com熱心網友回復:
command每次用戶更改檢查按鈕的狀態時,您可以使用檢查按鈕屬性呼叫函式來實作此目的。
def show_hide_func():
if cb_val == 1:
your_label.pack_forget()
# if you are using grid() to create, then use grid_forget()
elif cb_val == 0:
your_label.pack()
cb_val = tk.IntVar()
tk.Checkbutton(base_window, text="Click me to toggle label", variable=cb_val , onvalue=1, offvalue=0, command=show_hide_func).pack()
有關檢查按鈕和其他 Tkinter 小部件的詳細檔案,請閱讀此處
uj5u.com熱心網友回復:
只需使用comman=function來運行隱藏(pack_forget()/ grid_forget()/ place_forget())并再次顯示(pack()/ grid(...)/ place(...))的代碼。
Withpack()可能是問題,因為它會再次顯示它,但在其他小部件的末尾 - 所以你可以保留Label其中Frame不會有其他小部件。或者您可以使用pack(before=checkbox)(或類似選項)再次放在同一個地方(之前checkbox)
Label里面Frame
import tkinter as tk
# --- functions ---
def on_click():
print(checkbox_var.get())
if checkbox_var.get():
label.pack_forget()
else:
label.pack()
# --- main ---
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
label = tk.Label(frame, text='Hello World')
label.pack()
checkbox_var = tk.BooleanVar()
checkbox = tk.Checkbutton(root, text="select", variable=checkbox_var, command=on_click)
checkbox.pack()
root.mainloop()
采用pack(before=checkbox)
import tkinter as tk
# --- functions ---
def on_click():
print(checkbox_var.get())
if checkbox_var.get():
label.pack_forget()
else:
label.pack(before=checkbox)
# --- main ---
root = tk.Tk()
label = tk.Label(root, text='Hello World')
label.pack()
checkbox_var = tk.BooleanVar()
checkbox = tk.Checkbutton(root, text="select", variable=checkbox_var, command=on_click)
checkbox.pack()
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/439076.html
