在register子例程中,registerscreencanvas
对象具有属性button2。此按钮是一个后退按钮,可将用户带回到startscreencanvas
画布并删除registerscreencanvas
画布。
如何使button2命令删除registerscreencanvas
对象/画布并创建/打开startscreencanvas
对象/画布?
代码:
from tkinter import *
root = Tk()
root.geometry("600x600")
root.title("Maths test program")
root.configure(background='#82f2ff')
photo1 = PhotoImage(file="startlabelphoto.png")
photo2 = PhotoImage(file="usernamelabelphoto.png")
photo3 = PhotoImage(file="passwordlabelphoto.png")
photo4 = PhotoImage(file="registerlabelphoto.png")
def login():
global usernameentryentry
usernameentryentry = startscreencanvas.usernameentry.get()
global passwordentryentry
passwordentryentry = startscreencanvas.passwordentry.get()
if usernameentryentry == "Tom" and passwordentryentry == "Tom's password":
standardcanvas.destroy(startscreencanvas)
def register():
print('register button has been pressed')
standardcanvas.destroy(startscreencanvas)
global registerscreencanvas
registerscreencanvas = logincanvas(root, photo4, "Register", examplesubroutine, "Back",
examplesubroutine, "loginscreen")
teachersonlylabel = Label(registerscreencanvas.canvas, text="(For teachers only)",
fg="black", font=("Arial", 30)).place(x=0, y=100, width=600, height=50)
def examplesubroutine():
print("Button clicked")
class standardcanvas:
def __init__(self, master, titleimage, button1text, button1command, button2text, button2command, screen):
self.master = master
self.canvas = Canvas(root, width=610, height=610, bg='#82f2ff', borderwidth=0, highlightthickness=0)
self.canvas.pack()
self.titlelabel = Label(self.canvas, image=titleimage)
self.titlelabel.place(x=0, y=20, width=600, height=50)
self.button1 = Button(self.canvas, text=button1text, command=button1command, relief = 'groove', font=("Ariel", 30))
self.button1.place(x=100, y=550)
self.button2 = Button(self.canvas, text=button2text, command=button2command, relief = 'groove', font=("Ariel", 30))
self.button2.place(x=350, y=550)
def destroy(self):
self.canvas.destroy()
class logincanvas(standardcanvas):
def __init__(self, master, titleimage, button1text, button1command, button2text, button2command, screen):
standardcanvas.__init__(self, master, titleimage, button1text, button1command, button2text, button2command, screen)
self.usernamelabel = Label(self.canvas, image=photo2, relief = 'flat')
self.usernamelabel.place(x=95, y=250, width=175, height=50)
self.usernameentry = Entry(self.canvas, relief = 'groove', font=("Ariel", 30))
self.usernameentry.place(x=275, y=250, width=200, height=50)
self.passwordlabel = Label(self.canvas, image=photo3, relief = 'flat')
self.passwordlabel.place(x=95, y=300, width=175, height=50)
self.passwordentry = Entry(self.canvas, relief = 'groove', show="*", font=("Ariel", 30))
self.passwordentry.place(x=275, y=300, width=200, height=50)
def startscreen():
global startscreencanvas
startscreencanvas = logincanvas(root, photo1, "Login", login, "Register", register, "loginscreen")
startscreen()
root.mainloop()