如何在作为类对象的画布之间切换?

时间:2019-02-22 21:40:36

标签: python tkinter

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

0 个答案:

没有答案