Tkinter小部件无法删除(由for循环创建)

时间:2014-06-10 10:04:25

标签: python for-loop tkinter widget

我用for循环创建了各种Tkinter小部件。所有这些都删除很好,除了" lock_btn"小部件。当我按下该按钮时,锁定按钮停留在页面上(尽管该功能中的其余代码可用)。我已尝试使用和不使用全局变量(您看到的代码包括全局变量)。

import tkinter
import tkinter.messagebox

#Setting properties for the window
window = tkinter.Tk()
window.title("Shutdown Timer")
window.geometry("250x300")
window.configure(background="black")

def Login():
    for x in range(0,5):
        login_window[x].pack_forget()


def Auth():
    if usr.get() == "isensedemons":
        if pas.get() == password:
            Login()
        else:
            tkinter.messagebox.showinfo("Login Error", "Incorrect Username or Password")
    else:
        tkinter.messagebox.showinfo("Login Error", "Incorrect Username or Password")

def Lock():
    global lock_btn
    for x in range(0,1):
        lock_btn.pack_forget() 

    for x in range(0,5):
        login_window[x].pack()


lock_btn = tkinter.Button(window, text="Lock", fg="white", bg="black", command=Lock)
lbl_usr = tkinter.Label(window, text="Username", fg="white", bg="black")
usr = tkinter.Entry(window)
lbl_pas = tkinter.Label(window, text="Password", fg="white", bg="black")
pas = tkinter.Entry(window, show="•")
btn = tkinter.Button(window, text="Authenticate", fg="white", bg="black", command=Auth)
password = "password"
login_window = [lbl_usr,usr,lbl_pas,pas,btn]

class Create():
    lock_btn.pack()
    lock_btn.place(rely=1, relx=1, anchor="se")

Create()
#Starts the Program
window.mainloop()

1 个答案:

答案 0 :(得分:1)

首先拨打lock_btn.pack(0),然后切换到使用place(...)。因此,窗口小部件由场所管理,因为它只能由一个几何管理器管理。当您致电pack_forget时,它无效,因为pack无法控制小部件。