2014-06-10 81 views
0

我創建了各種帶for循環的Tkinter小部件。除了「lock_btn」小部件外,它們都可以很好地移除。當我按下該按鈕時,鎖定按鈕停留在頁面上(儘管該功能中的其餘代碼可以工作)。我曾嘗試使用和不使用全局變量(您看到的代碼包含全局變量)。Tkinter小部件無法刪除(由for循環創建)

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

你開始通過調用lock_btn.pack(0),然後切換到使用place(...)。因此,該小部件由地點管理,因爲它只能由一個幾何管理器管理。當您撥打pack_forget時,它不起作用,因爲pack不受控件的控制。

+0

感謝那個男人!任何想法如何我會解決這個問題,仍然能夠放置小部件? – user2880853

+0

沒關係,我解決了您的初始評論問題。謝謝! – user2880853