图像出现在Tkinter的错误窗口上



我正在创建一个mysql/tkinter项目,并试图使用一张图片作为其中一个窗口的bg。但它不是出现在新窗口上,而是在主窗口中打开。我希望图像在我当前打开的新窗口上打开。菜单((本身按我的意愿运行,但对于整个程序来说,它不起作用。关于我该怎么解决这个问题有什么想法吗?

def menu():
root = tk.Tk()
root.title("DBMS Menu Page")
bgimage= ImageTk.PhotoImage(Image.open('960x0.jpg'))
imglabel= Label(image=bgimage)
imglabel.img=bgimage
imglabel.pack()

lblmenu = tk.Label(root,text ="MENU", font=('Times New Roman',28,'bold'))
lblmenu.place(x = 220, y = 60) 
btninsert = tk.Button(root, text ="Insert", 
fg ='blue',font=("Times New Roman Bold", 10)) 
btninsert.place(x = 100, y = 135, width = 60)
btnupdate = tk.Button(root, text ="Update", 
fg ='blue',font=("Times New Roman Bold", 10)) 
btnupdate.place(x = 170, y = 135, width = 60)
btndelete = tk.Button(root, text ="Delete", 
fg ='blue',font=("Times New Roman Bold", 10)) 
btndelete.place(x = 240, y = 135, width = 60)
btndisplay = tk.Button(root, text ="Display", 
fg ='blue',font=("Times New Roman Bold", 10))
btndisplay.place(x = 310, y = 135, width = 60)


def clearlogin():
txtUser.delete(0,END)
txtpass.delete(0,END)

def submitact(): 

user = txtUser.get() 
passw = txtpass.get() 
print(f"The ID-pw entered by you is {user} {passw}") 
logintodb(user, passw) 
def logintodb(user, passw):
if passw: 
db = mys.connect(host ="localhost", 
user = user, 
password = passw, 
db ="1234") 
cursor = db.cursor() 
else: 
db = mys.connect(host ="localhost", 
user = root, 
database ="1234") 
cursor = db.cursor() 

# A Table in the database 
savequery = "show tables"

try: 
cursor.execute(savequery) 
myresult = cursor.fetchall() 
print("Query Excecuted succesfully")
messagebox.showinfo("Login ", "Login Successful")

except: 
db.rollback() 
print("Error occured") 
menu()    

root = tk.Tk() 
root.geometry("250x150") 
root.title("DBMS Login Page") 
lbluser = tk.Label(root, text ="Username -",fg="red",font=("Times New Roman Bold", 13) ) 
lbluser.place(x = 10, y = 20) 
txtUser = tk.Entry(root, width = 35,fg="green",font=("Times New Roman Bold", 10)) 
txtUser.place(x = 100, y = 20, width = 100) 
lblpass = tk.Label(root, text ="Password -",fg="red",font=("Times New Roman Bold", 13)) 
lblpass.place(x = 10, y = 50) 
txtpass = tk.Entry(root,show="*", width = 35,fg="green",font=("Times New Roman Bold", 10)) 
txtpass.place(x = 100, y = 50, width = 100) 

submitbtn = tk.Button(root, text ="Login", 
fg ='blue', font=("Times New Roman Bold", 15),command = submitact) 
submitbtn.place(x = 30, y = 100, width = 55)
clearbtn = tk.Button(root, text ="Clear", 
fg ='blue', font=("Times New Roman Bold", 15),command = clearlogin) 
clearbtn.place(x = 130, y = 100, width = 55)
root.mainloop()   

函数menu的第一部分是错误所在:

def menu():
root = Toplevel()
root.title("DBMS Menu Page")
bgimage= ImageTk.PhotoImage(Image.open('960x0.jpg'))
imglabel= Label(root,image=bgimage)
imglabel.img=bgimage
imglabel.pack()

你可以指定图像应该出现在哪个窗口,并将其添加为一个参数,解决了这个问题,并将根表示为Toplevel()比调用Tk()更好,后者通常只用于主父窗口

最新更新