3

Hi I am developing a single login in python using Tkinter(), I just only want that when the user Logged in correctly the login UI will be hidden and the content UI will display... so I was thinking that the ui can be hide or the visibility will be hidden? for example I had this code...

def httpGETCheck(username, password):
    .... 
    # assumed that the user is correct
    if registeredUser:
        # how can I hide or set visible is false to all the UI in the showLogin() Module
        # and how can I show the showContent() UI or replaced the showContent() UI from showLogin() UI

class ContentUI():
    def showContent(self, frame):
        button1 = Button(frame, text="+", width=15, command=counterPlus)
        button1.grid()

    def showLogin(self, frame):
        self.contentUI = ContentUI()    

        L1 = Label(frame, text="User Name")
        L1.pack( side = LEFT)
        L1.grid()

        E1 = Entry(frame, bd =5)
        E1.pack(side = RIGHT)
        E1.grid()

        L2 = Label(frame, text="Password")
        L2.pack( side = LEFT)
        L2.grid()       

        E2 = Entry(frame, bd =5, show="*")
        E2.pack(side = RIGHT)
        E2.grid()

        submit = Button(frame, text="Login", width=15, command=lambda: httpGETCheck(E1.get(), E2.get())) 
        submit.grid()

class UIDisplay():
    def play(self):
        root = Tk()

        root.title(title)
        root.geometry(dimension)

        app = Frame(root)

        contentUI = ContentUI()
        contentUI.showLogin(app)

        app.grid()


        root.mainloop()

ad = UIDisplay()
ad.play()
1

1 Answer 1

2

Here is an alternative: Define each "page" in a Frame. Then use calls to lift to raise the appropriate Frame above the others. (The following code borrows heavily from Bryan Oakley's code here.)

import tkinter as tk
class Page(tk.Frame):
    def __init__(self, master, text, height, width, *args, **kwargs):
        tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
        self.height = height
        self.width = width
        button = tk.Button(self, text=text, font=('Comic Sans MS', 20),
                           command=lambda: self.callback())
        button.pack(side="top", fill="both", expand=True)
    def onlift(self):
        root.geometry('{}x{}'.format(self.width, self.height))
        self.lift()

class App(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        p1 = Page(self, 'Login 1', height=200, width=300)
        p2 = Page(self, 'Next page is 2', height=400, width=300)
        p3 = Page(self, 'We love number 3', height=400, width=600)
        p1.callback = p2.onlift
        p2.callback = p3.onlift
        p3.callback = p1.onlift

        p1.place(x=0, y=0, relwidth=1, relheight=1)
        p2.place(x=0, y=0, relwidth=1, relheight=1)
        p3.place(x=0, y=0, relwidth=1, relheight=1)

        p1.onlift()

root = tk.Tk()
app = App(root)
root.mainloop()

Above I've only defined one kind of Frame, called Page. For your situation, you'd want a Login Frame, and then a subsequent Frame:

    p1 = Login(self, ...)
    p2 = Registered(self, ...)

Sign up to request clarification or add additional context in comments.

1 Comment

Just for documentation for user of other programm languages (but mentioned briefly in the answer): lift() calls the tk command raise to change the z order of the widget`s window. See the tk doc for raise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.