2017-12-16 10 views
1

새로운 윈도우를 열지 말고 현재 윈도우를 내용물로 대체하고 싶습니다. 새 창을 열 때마다 버튼을 눌러에서 볼 수 있듯이윈도우 교체 방법 TKInter

from tkinter import * 

def adminLogin(): 
    global AnameEL 
    global ApwordEL # More globals :D 
    global ArootA 

    ArootA = Tk() # This now makes a new window. 
    ArootA.geometry('1280x720') 
    ArootA.title('Admin login') # This makes the window title 'login' 

    f1 = Frame(width=200, height=200, background="#D3D3D3") 
    f2 = Frame(ArootA, width=400, height=200) 

    f1.pack(fill="both", expand=True, padx=0, pady=0) 
    f2.place(in_=f1, anchor="c", relx=.5, rely=.5) 

    AnameL = Label(f2, text='Username: ') # More labels 
    ApwordL = Label(f2, text='Password: ') #^
    AnameL.grid(row=1, sticky=W) 
    ApwordL.grid(row=2, sticky=W) 

    AnameEL = Entry(f2) # The entry input 
    ApwordEL = Entry(f2, show='*') 
    AnameEL.grid(row=1, column=1) 
    ApwordEL.grid(row=2, column=1) 

    AloginB = Button(f2, text='Login', command=CheckAdmin) # This makes the login button, which will go to the CheckLogin def. 
    AloginB.grid(columnspan=2, sticky=W) 

def CheckAdmin(): 
    if AnameEL.get() == "test" and ApwordEL.get() == "123" : # Checks to see if you entered the correct data. 
     r = Tk() # Opens new window 
     r.title('Sucess') 
     loginC = Button(r, text='Add new login', command=Signup) 
     loginC.grid(columnspan=2, sticky=W) 
     r.mainloop() 
    else: 
     r = Tk() 
     r.title('Error') 
     r.geometry('550x450') 
     rlbl = Label(r, text='\n[!] Invalid Login') 
     rlbl.pack() 
     r.mainloop() 

def Signup(): # This is the signup definition, 
    global pwordE # These globals just make the variables global to the entire script, meaning any definition can use them 
    global nameE 
    global roots 

    roots = Tk() # This creates the window, just a blank one. 
    roots.title('Signup') # This renames the title of said window to 'signup' 
    intruction = Label(roots, text='Please Enter new Credidentials\n') # This puts a label, so just a piece of text saying 'please enter blah' 
    intruction.grid(row=0, column=0, sticky=E) # This just puts it in the window, on row 0, col 0. If you want to learn more look up a tkinter tutorial :) 

    nameL = Label(roots, text='New Username: ') # This just does the same as above, instead with the text new username. 
    pwordL = Label(roots, text='New Password: ') # ^^ 
    nameL.grid(row=1, column=0, sticky=W) # Same thing as the instruction var just on different rows. :) Tkinter is like that. 
    pwordL.grid(row=2, column=0, sticky=W) # ^^ 

    nameE = Entry(roots) # This now puts a text box waiting for input. 
    pwordE = Entry(roots, show='*') # Same as above, yet 'show="*"' What this does is replace the text with *, like a password box :D 
    nameE.grid(row=1, column=1) # You know what this does now :D 
    pwordE.grid(row=2, column=1) # ^^ 

    signupButton = Button(roots, text='Signup', command=FSSignup) # This creates the button with the text 'signup', when you click it, the command 'fssignup' will run. which is the def 
    signupButton.grid(columnspan=2, sticky=W) 
    roots.mainloop() # This just makes the window keep open, we will destroy it soon 

adminLogin() 

: 여기 내 코드입니다. 대신 현재 창을 바꾸고 싶습니다. 각 단추를 누르면 새 창이 열립니다. 대신 현재 창을 바꾸길 원합니다.

도움 주셔서 감사합니다.

답변

1

CheckAdmin()에서 새 창을 만드는 대신 f1을 삭제 한 다음 ArootA 내부에 새 프레임을 만들거나 ArootA를 변경하십시오. CheckAdmin()의 잘못된 경우, 예를 들어

: 또한 F1을 필요

f1.destroy() # Removes everything currently inside ArootA. 
    ArootA.geometry('550x450') 
    ArootA.title('Error') 
    rlbl = Label(ArootA, text='\n[!] Invalid Login') 
    rlbl.pack() 

글로벌한다. 또한 창을 열려면 adminLogin() 끝에 ArootA.mainloop()을 추가해야했습니다.

+0

감사합니다. @ sn8wman,이 작동합니다! –

0

현재 창을 대체하는 코드를 다시 작성했지만, 사용자가 요청한 것인지 확실하지 않습니다. pls 그것을 밖으로 시도, 각 함수 호출에 대한 창을 바꾸지 않지만 여전히 하나의 루트 창을 유지합니다.

#%% 
from tkinter import * 

def adminLogin(): 
    global AnameEL 
    global ApwordEL # More globals :D 
    global ArootA 

    AnameL = Label(f2, text='Username: ') # More labels 
    ApwordL = Label(f2, text='Password: ') #^
    AnameL.grid(row=1, sticky=W) 
    ApwordL.grid(row=2, sticky=W) 

    AnameEL = Entry(f2) # The entry input 
    ApwordEL = Entry(f2, show='*') 
    AnameEL.grid(row=1, column=1) 
    ApwordEL.grid(row=2, column=1) 

    AloginB = Button(f2, text='Login', command=CheckAdmin) # This makes the 
    login button, which will go to the CheckLogin def. 
    AloginB.grid(columnspan=2, sticky=W) 


def CheckAdmin(): 
    if AnameEL.get() == "test" and ApwordEL.get() == "123" : # Checks to see 
    if you entered the correct data. 
    f2.destroy() 
    #r = Tk() # Opens new window 
    loginL = Label(f3, text = 'Success!!!') 
    loginC = Button(f3, text='Add new login', command=Signup) 
    loginL.grid(row = 2, column=0, sticky=W) 
    loginC.grid(columnspan=2, sticky=E) 
    #r.mainloop() 
else: 
    f2.destroy() 
    #r = Tk() # Opens new window 
    loginL2 = Label(f3, text = 'Error!!') 
    ribl = Label(f3, text='\n[!] Invalid Login') 
    loginL2.grid(row = 2, column=0, sticky=W) 
    ribl.grid(row = 3, column=0, sticky=W) 

    #.mainloop() 


def Signup(): # This is the signup definition, 
    global pwordE # These globals just make the variables global to the 
    entire script, meaning any definition can use them 
    global nameE 
    global roots 

    f3.destroy() 
    #r = Tk() # Opens new window 
    loginL3 = Label(f4, text = 'Signup!!!') # This renames the title of said 
    window to 'signup' 
    loginL3.grid(row = 0, column=50, sticky=W) 

    intruction = Label(f4, text='Please Enter new Credidentials\n') # This 
    puts a label, so just a piece of text saying 'please enter blah' 
    intruction.grid(row=2, column=0, sticky=E) # This just puts it in the 
    window, on row 0, col 0. If you want to learn more look up a tkinter 
    tutorial :) 

    nameL = Label(f4, text='New Username: ') # This just does the same as 
    above, instead with the text new username. 
    pwordL = Label(f4, text='New Password: ') # ^^ 
    nameL.grid(row=3, column=0, sticky=W) # Same thing as the instruction 
    var just on different rows. :) Tkinter is like that. 
    pwordL.grid(row=4, column=0, sticky=W) # ^^ 

    nameE = Entry(f4) # This now puts a text box waiting for input. 
    pwordE = Entry(f4, show='*') # Same as above, yet 'show="*"' What this 
    does is replace the text with *, like a password box :D 
    nameE.grid(row=3, column=1) # You know what this does now :D 
    pwordE.grid(row=4, column=1) # ^^ 

    signupButton = Button(f4, text='Signup', command=FSSignup) # This 
    creates 
    the button with the text 'signup', when you click it, the command 
    'fssignup' 
    will run. which is the def 
    signupButton.grid(columnspan=2, sticky=W) 
    roots.mainloop() # This just makes the window keep open, we will 
    destroy it soon 

    ArootA = Tk() # This now makes a new window. 
    ArootA.geometry('1280x720') 
    ArootA.title('Admin login') # This makes the window title 'login' 

    f1 = Frame(width=200, height=200, background="#D3D3D3") 
    f2 = Frame(ArootA, width=400, height=200) 

    f2.pack(fill='both', expand=True, padx=0, pady=0, side = TOP) 
    #f2.place(in_=f1, anchor="c", relx=.5, rely=.5) 
    f3 = Frame(ArootA, width=550, height=450) 
    f3.pack(fill='both', expand=True, padx=0, pady=0, side = TOP) 
    f4 = Frame(ArootA, width=550, height=450) 
    f4.pack(fill='both', expand=True, padx=0, pady=0, side = TOP) 

    adminLogin() 

    ArootA.mainloop() 
+0

안녕하세요, Grace 님의 회신에 감사드립니다. 들여 쓰기와 주석이 같은 줄 오류가 아닌 것 같습니다. 너이 뜻인 것 같니? https://pastebin.com/raw/7CXb28Wd 그렇다면 다음과 같은 오류가 발생합니다. "NameError : name 'f2'not defined" –

+0

안녕하세요 qtt qtt 올바르게 보낸 링크에 올바른 버전이 있으므로 오른쪽 붙여 넣기를 다시했습니다. 여기 하나. 그것은 어떤 오류없이 아주 잘 작동합니다. https://pastebin.com/tYh9kz4J –

+0

안녕하세요 qtt qtt, 올바른 링크 버전을 확인하십시오 https://pastebin.com/tYh9kz4J, 붙여 넣기가 올바르지 않습니다. 이 하나의 오류없이 아주 잘 작동 –

1

코드가 설계된 방식 때문에 여러 창이 나타납니다. 주된 루틴은 adminLogin(), CheckAdmin()Signup()입니다. 각각의 루틴은 Tk()을 호출하여 새로운 "루트"또는 "마스터"창을 생성하며 호출 될 때마다이를 수행합니다. 응용 프로그램에서는 하나의 마스터 창 (하나만 Tk())을 생성해야합니다. 그런 다음 해당 마스터 창을 세 가지 기능 각각에 전달할 수 있습니다. 또는이를 글로벌로 사용하십시오.

.grid_forget() 또는 .grid_remove()으로 전화하여 .grid()으로 가져온 위젯을 숨기거나 제거 할 수도 있습니다.

+0

정보 주셔서 감사합니다 게리! –