我正在使用 tkraise() 在登录页面和注册页面之间切换,但问题是我的代码的第一部分(注册),它没有显示背景图像,而第二部分(登录) ) 工作正常。 有人可以帮我解决这个问题吗?太感谢了。 这是我的代码:
from tkinter import *
from PIL import ImageTk
accountSystem=Tk()
accountSystem.rowconfigure(0, weight=1)
accountSystem.columnconfigure(0, weight=1)
accountSystem.geometry('1280x720+128+72')
accountSystem.title('ACCOUNT SYSTEM')
#Navigating through windows
sign_in=Frame(accountSystem)
sign_up=Frame(accountSystem)
for frame in (sign_in, sign_up):
frame.grid(row=0, column=0, sticky='nsew')
def show_frame(frame):
frame.tkraise()
show_frame(sign_up)
#-------------------------------------------------------------------
--
#-------------------------FUNCTIONALLY PART-------------------------
#---------------------------------------------------------------------
def hide():
openeye.config(file='images/close_eye.png')
passwordEntry.config(show='*')
eyeButton.config(command=show)
def show():
openeye.config(file='images/open_eye.png')
passwordEntry.config(show='')
eyeButton.config(command=hide)
def user_enter(event):
if usernameEntry.get()=='Username':
usernameEntry.delete(0, END)
def password_enter(event):
if passwordEntry.get()=='Password':
passwordEntry.delete(0, END)
def confirmpassword_enter(event):
if confirmpasswordEntry.get()=='Conform Password':
confirmpasswordEntry.delete(0, END)
#-------------------------------------------------------------------
#----------------SIGN UP----------------------------------------------
#---------------------------------------------------------------------
#-----BACKGROUND IMAGE-----
bgImage=ImageTk.PhotoImage(file='images/1_signup.png')
bgLabel=Label(sign_up, image=bgImage)
bgLabel.place(x=0, y=0)
heading=Label(sign_up, text='Sign up', font=('Open Sans', 26, 'bold') ,bg='SlateBlue1', fg='white')
heading.place(x=795, y=188)
#-----USERNAME-----
usernameEntry=Entry(sign_up, width=27, font=('Open Sans', 14, 'bold'),bg='SlateBlue1',fg='white', bd=0)
usernameEntry.place(x=705, y=275)
usernameEntry.insert(0, 'Username')
usernameEntry.bind('<FocusIn>', user_enter)
#-----PASSWORD-----
passwordEntry=Entry(sign_in, width=25, font=('Open Sans', 14, 'bold'),bg='SlateBlue1',fg='white', bd=0)
passwordEntry.place(x=705, y=340)
passwordEntry.insert(0, 'Password')
passwordEntry.bind('<FocusIn>', password_enter)
openeye=PhotoImage(file='images/open_eye.png')
eyeButton=Button(sign_up, image=openeye, bd=0, bg='SlateBlue1', activebackground='SlateBlue1',cursor='hand2', command=hide)
eyeButton.place(x=980, y=340)
#-----CONFIRM PASSWORD-----
confirmpasswordEntry=Entry(sign_up, width=25, font=('Open Sans', 14, 'bold'),bg='SlateBlue1',fg='white', bd=0)
confirmpasswordEntry.place(x=705, y=410)
confirmpasswordEntry.insert(0, 'Conform Password')
confirmpasswordEntry.bind('<FocusIn>', confirmpassword_enter)
#REGISTER BUTTON----
registerButton=Button(sign_up, text='Register', font=('Open Sans', 16, 'bold') ,bg='SlateBlue1', fg='white', activeforeground='white', activebackground='SlateBlue1', cursor='hand2', bd=0)
registerButton.place(x=815, y=475)
#----SWITCH INTO LOGIN PAGE-----
loginLabel=Label(sign_up, text="Already have an account?", font=('Open Sans', 9, 'bold'),bg='SlateBlue3', fg='white', bd=0)
loginLabel.place(x=770,y=532)
loginButton=Button(sign_up, text='Login', font=('Open Sans', 9, 'bold underline'),fg='white', bg='SlateBlue3', activeforeground='white', activebackground='SlateBlue3', cursor='hand2', bd=0, command=lambda : show_frame(sign_in))
loginButton.place(x=920, y=530)
#---------------------------------------------------------------------
#----------------SIGN IN----------------------------------------------
#---------------------------------------------------------------------
#-----BACKGROUND IMAGE-----
bgImage=ImageTk.PhotoImage(file='images/1_signin.png')
bgLabel=Label(sign_in, image=bgImage)
bgLabel.place(x=0, y=0)
heading=Label(sign_in, text='Sign in', font=('Open Sans', 26, 'bold') ,bg='SlateBlue1', fg='white')
heading.place(x=815, y=190)
#-----USERNAME------
usernameEntry=Entry(sign_in, width=27, font=('Open Sans', 14, 'bold'),bg='SlateBlue1',fg='white', bd=0)
usernameEntry.place(x=720, y=305)
usernameEntry.insert(0, 'Username')
usernameEntry.bind('<FocusIn>', user_enter)
#-----PASSWORD-----
passwordEntry=Entry(sign_in, width=25, font=('Open Sans', 14, 'bold') ,bg='SlateBlue1',fg='white', bd=0)
passwordEntry.place(x=720, y=380)
passwordEntry.insert(0, 'Password')
passwordEntry.bind('<FocusIn>', password_enter)
openeye=PhotoImage(file='images/open_eye.png')
eyeButton=Button(sign_in, image=openeye, bd=0, bg='SlateBlue1', activebackground='SlateBlue1'
,cursor='hand2', command=hide)
eyeButton.place(x=1000, y=380)
#-----FORGET PASSWORD-----
forgetButton=Button(sign_in, text='Forget Password?', bd=0, bg='SlateBlue3', activebackground='SlateBlue3',cursor='hand2', font=('Open Sans', 10, 'bold'), fg='white', activeforeground='white')
forgetButton.place(x=715, y=460)
#-----lOGIN BUTTON-----
loginButton=Button(sign_in, text='Login', font=('Open Sans', 16, 'bold') ,bg='SlateBlue1', fg='white', activeforeground='white', activebackground='SlateBlue1', cursor='hand2', bd=0)
loginButton.place(x=925, y=455)
#-----SWITCH INTO REGISTER-----
signupLabel=Label(sign_in, text="Don't have an account?", font=('Open Sans', 9, 'bold'), bg='SlateBlue3', fg='white', bd=0)
signupLabel.place(x=730, y=522)
signupButton=Button(sign_in, text='Creat new account', font=('Open Sans', 9, 'bold underline'),fg='white', bg='SlateBlue3', activeforeground='white', activebackground='SlateBlue3', cursor='hand2', bd=0, command=lambda : show_frame(sign_up))
signupButton.place(x=870, y=520)
accountSystem.resizable(0,0)
accountSystem.mainloop()
我更改了登录和注册部分的顺序(将登录部分放在注册部分上方)。但无论我更改了哪一部分,它始终是代码的第一部分,其背景图像没有显示。