0

My current code is found at the bottom

I am trying to close Window1 after successful log in has been made and open Window2

Currently I have username entry only just for test purposes.

My current if statement opens up new window if one is the username Entry. But two windows are displayed.

I need to get rid of Window 1 and just show Window 2 after successful log in.

I have done some research and found this answer but I don't think this is functional with my current code.

I also tried the following but an error shows up:

def login_system(self):
    u = self.UsernameEntry.get()
    if u == "one":
        self.newWindow = Toplevel(self.master)
        self.app = Window2(self.newWindow)
        Window1().destory()

Error :

TypeError: __init__() missing 1 required positional argument: 'master'

How can I achieve this?

from tkinter import *
from tkinter import DISABLED, messagebox
import tkinter.ttk as ttk
import os
import glob
from PIL import Image, ImageTk, ImageGrab
from pathlib import Path
import pyautogui
import time

def main():
    root = Tk()
    app = Window1(root)
    root.mainloop()

class Window1:
    def __init__(self,master):
        self.master = master
        self.master.title("User Log In")
        self.master.geometry('1000x750')
        self.frame = Frame(self.master)
        self.frame.pack(fill="both", expand=True)


        self.UsernameEntry = Entry(self.frame)
        self.UsernameEntry.grid(row = 2, column = 0)


        self.btnLogin = Button(self.frame, text = 'login', width = 17, command = self.login_system)
        self.btnLogin.grid(row = 3, column = 0)

    def login_system(self):
        u = self.UsernameEntry.get()
        if u == "one":
            self.newWindow = Toplevel(self.master)
            self.app = Window2(self.newWindow)

        else:
            self.UsernameEntry.delete(0,"end")


class Window2:
    def __init__(self,master):
        notebook = ttk.Notebook(master)

        notebook.pack(expand = 1, fill = "both")
        #Frames
        main = ttk.Frame(notebook)
        manual = ttk.Frame(notebook)
        notebook.add(main, text='Main-Screen')
        notebook.add(manual, text='Manual')



if __name__ == '__main__':
    main()

1 Answers1

2

You are using many libraries that you dont need. Anyway you just need one line of code for this to work Inside if statement self.master.withdraw()

--EDIT-- Because you asked for it, i added a function to close the cmd

Full code

from tkinter import *
...

def main():
    root = Tk()
    app = Window1(root)
    root.mainloop()

class Window1:
    def __init__(self,master):
        self.master = master
        ....

    self.btnLogin = Button(self.frame, text = 'login', width = 17, command = self.login_system)
    self.btnLogin.grid(row = 3, column = 0)

    def on_closing(self): #Add this event handler
        self.master.destroy()

    def login_system(self):
        u = self.UsernameEntry.get()
        if u == "one":
            self.master.withdraw() #Do NOT call destroy as you need the root to be 
                                   #active for Toplevel
            self.newWindow = Toplevel(self.master)
            self.newWindow.protocol("WM_DELETE_WINDOW", self.on_closing) #And add this

            self.app = Window2(self.newWindow)

        else:
            self.UsernameEntry.delete(0,"end")


class Window2:
    ....



if __name__ == '__main__':
    main()
Jordan
  • 525
  • 1
  • 4
  • 19
  • Nice one Jordan! The Libaries are needed for rest of my code, creating something thats all. But appreciate the answer! Do you have any suggestions how to store passwords and usernames? For local use only –  Sep 29 '19 at 12:13
  • Are you talking about a database? If that is the case then the simplest would be to just use a json to store them. If you need them online there are many free databases you can use (I have used firebase for android messaging application with login and it was easy and useful) – Jordan Sep 29 '19 at 12:19
  • Currently I am storing username and password inside a .txt file. I'll probably need something else –  Sep 29 '19 at 12:21
  • Well the txt isnt really reliable thats why i dont recommend it. Howerver it is a good point to start. As i said i think firebase is a good option for begginers. Check it out and tell my if you like it. – Jordan Sep 29 '19 at 12:23
  • Awesome thanks @Jordan also, regarding the code above. `root.withdraw()` cmd is still running, should that be a concern after the whole app is closed? –  Sep 29 '19 at 12:25
  • I dont think that it is a concern, but there is a way you can deal with it.The cmd is not closing because Tk is still running in the background. You can close it by adding a handler to the second window. See this answer for more details https://stackoverflow.com/a/111160/9220253 I also edited my code so check it – Jordan Sep 29 '19 at 12:48
  • As I said you can see my code, I tested it and cmd is closed after closing the second window, if that's what you want. The protocol is placed when creating the window. – Jordan Sep 29 '19 at 12:56
  • `self.newWindow.protocol("WM_DELETE_WINDOW", self.on_closing) AttributeError: 'Window1' object has no attribute 'on_closing'` my error –  Sep 29 '19 at 13:01
  • Oh sorry my bad, just place the protocol one line down, now it works 100%(updated answer) – Jordan Sep 29 '19 at 13:04
  • sorry its still the same aha! –  Sep 29 '19 at 13:06
  • By one line down i mean exchange lines with Toplevel declaration. Obviously the window has to be declared before everything else. – Jordan Sep 29 '19 at 13:07
  • do you have an email or something where I can contact you? –  Sep 29 '19 at 13:47
  • i do have an email: iordanis231@hotmail.com but i dont use it often so maybe you could tell me your FB or Messenger account if you have. – Jordan Sep 29 '19 at 14:50
  • instagram? don't have either –  Sep 29 '19 at 14:52
  • iordanis_sap, i have a photo with sunglasses – Jordan Sep 29 '19 at 15:00