如何替换window TKInter
How to replace window TKInter
我希望用内容替换当前的 window,而不是打开新的 window。这是我的代码:
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()
如您所见,按每个按钮都会打开一个新的 windows。我想替换当前的 window instead.As 您可以看到在每个按钮按下时都会打开一个新的 windows。我希望替换当前的 window。
感谢您的帮助。
而不是在 CheckAdmin() 中创建一个新的 window 销毁 f1,然后在 ArootA 中创建一个新框架或更改 ArootA。
例如 CheckAdmin() 的无效情况:
f1.destroy() # Removes everything currently inside ArootA.
ArootA.geometry('550x450')
ArootA.title('Error')
rlbl = Label(ArootA, text='\n[!] Invalid Login')
rlbl.pack()
这也要求 f1 是全局的。我还需要在 adminLogin() 末尾添加 ArootA.mainloop() 以打开 window。
我已经重写了代码以替换当前的 window,但我不确定这是否是您所要求的。请尝试一下,它确实为每个函数调用替换了 window,但仍然保留一个根 window.
#%%
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()
由于您的代码设计方式,您得到了多个 windows。您有三个主要例程:adminLogin()
、CheckAdmin()
和 Signup()
。这些例程中的每一个都创建一个新的 "root" 或 "master" window 并调用 Tk()
,并且它们在每次被调用时都会这样做。您的应用程序应该只创建一个主 window -- 一次调用 Tk()
。然后你可以将那个 master window 传递给你的三个函数中的每一个。或者将其用作全局。
您还可以通过调用 .grid_forget()
或 .grid_remove()
来隐藏或删除使用 .grid()
放置的小部件。
我希望用内容替换当前的 window,而不是打开新的 window。这是我的代码:
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()
如您所见,按每个按钮都会打开一个新的 windows。我想替换当前的 window instead.As 您可以看到在每个按钮按下时都会打开一个新的 windows。我希望替换当前的 window。
感谢您的帮助。
而不是在 CheckAdmin() 中创建一个新的 window 销毁 f1,然后在 ArootA 中创建一个新框架或更改 ArootA。
例如 CheckAdmin() 的无效情况:
f1.destroy() # Removes everything currently inside ArootA.
ArootA.geometry('550x450')
ArootA.title('Error')
rlbl = Label(ArootA, text='\n[!] Invalid Login')
rlbl.pack()
这也要求 f1 是全局的。我还需要在 adminLogin() 末尾添加 ArootA.mainloop() 以打开 window。
我已经重写了代码以替换当前的 window,但我不确定这是否是您所要求的。请尝试一下,它确实为每个函数调用替换了 window,但仍然保留一个根 window.
#%%
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()
由于您的代码设计方式,您得到了多个 windows。您有三个主要例程:adminLogin()
、CheckAdmin()
和 Signup()
。这些例程中的每一个都创建一个新的 "root" 或 "master" window 并调用 Tk()
,并且它们在每次被调用时都会这样做。您的应用程序应该只创建一个主 window -- 一次调用 Tk()
。然后你可以将那个 master window 传递给你的三个函数中的每一个。或者将其用作全局。
您还可以通过调用 .grid_forget()
或 .grid_remove()
来隐藏或删除使用 .grid()
放置的小部件。