如何替换窗口TKInter

时间:2017-12-16 23:15:12

标签: python python-3.x tkinter python-3.5

我希望将当前窗口替换为内容,而不是打开一个新窗口。这是我的代码:

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()

正如您在每个按钮上看到的那样,打开一个新窗口。我想要替换当前窗口。正如您在每个按钮上看到的那样,打开一个新窗口。我希望替换当前窗口。

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

不是在CheckAdmin()中创建一个新窗口,而是销毁f1,然后在ArootA内创建一个新框架或更改ArootA。

例如,在CheckAdmin()的无效情况下:

<th class="remarks">

这也要求f1成为全球性的。我还需要在adminLogin()的末尾添加ArootA.mainloop()以获得一个打开的窗口。

答案 1 :(得分:1)

You are getting multiple windows because of the way your code is designed. You have three main routines: adminLogin(), CheckAdmin(), and Signup(). Each of those routines creates a new "root" or "master" window with a call to Tk(), and they do it each time they're called. Your application should only create a single master window -- one call to Tk(). And then you can pass that master window to each of your three functions. Or use it as a global.

You can also hide or remove widgets that you placed with .grid() by calling .grid_forget() or .grid_remove().

答案 2 :(得分:0)

我已经重写了代码以替换当前窗口,但我不确定这是否是您要求的。尝试一下,它确实替换了每个函数调用的窗口,但仍保留一个根窗口。

<div id="devextreme2"></div>
<script>
    jQuery(function($){
        $("#devextreme2").dxCheckBox({
            "name": "RememberMe",
            "value": null, 
            "text":"Remember me?",
            "inputAttr": {"id":"RememberMe"}
        });
    });
</script>