1

I'm trying to make a GUI for a password storage and hashing system, but I have hit a roadblock. I am having 2 buttons, one to log in, and one to make an account. When the login button is clicked, a new tkinter window opens with the login page. however, the login button is supposed to show on the second page, but it doesn't, and I have no clue why. Here is the code for the full system:

import tkinter
from tkinter import*

username = ("Tom")
password = ("test") 
usernameguess1 = ("")
passwordguess1 = ("")


def trylogin():
   print ("Trying to login...")
   if usernameguess.get() == username:
       print ("Complete sucsessfull!")
       messagebox.showinfo("Sucess ", "Successfully logged in.")
   else:
       print ("Error: (Incorrect value entered)")
       messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")

def loginpage():
    #Gui Formatting
    window = tkinter.Tk()
    window.resizable(width=FALSE, height=FALSE)
    window.title("HasherTest_V0.1 Login")
    window.geometry("300x150")

    #Username and password boxes
    usernametext = tkinter.Label(window, text="Username:")
    usernameguess = tkinter.Entry(window)
    passwordtext = tkinter.Label(window, text="Password:")
    passwordguess = tkinter.Entry(window, show="*")

    usernameguess1 = usernameguess

    #login button

    attemptlogin = tkinter.Button(text="Login", command=trylogin)

    usernametext.pack()
    usernameguess.pack()
    passwordtext.pack()
    passwordguess.pack()
    attemptlogin.pack()
    window.mainloop()


#Gui Formatting (again)
window = tkinter.Tk() 
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1")
window.geometry("300x150")

loginbutton = tkinter.Button(text="Login", command=loginpage)
loginbutton.pack()

And here is the code for just the second window on its own. For some reason I have to import tkinter message boxes aswell, or my IDLE errors out.

import tkinter
from tkinter import *
from tkinter import messagebox

username = ("Tom")
password = ("test") 
usernameguess1 = ("")
passwordguess1 = ("")


def trylogin():
   print ("Trying to login...")
   if usernameguess.get() == username:
       print ("Complete sucsessfull!")
       messagebox.showinfo("Sucess ", "Successfully logged in.")
   else:
       print ("Error: (Incorrect value entered)")
       messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")

#Gui Formatting
window = tkinter.Tk() 
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1 Login")
window.geometry("300x150")


#Username and password boxes
usernametext = tkinter.Label(window, text="Username:")
usernameguess = tkinter.Entry(window)
passwordtext = tkinter.Label(window, text="Password:")
passwordguess = tkinter.Entry(window, show="*")

usernameguess1 = usernameguess

#login button
attemptlogin = tkinter.Button(text="Login", command=trylogin)

usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
window.mainloop()

Thanks for your help!

Tf0R24
  • 65
  • 1
  • 8
  • I saw multiple calls to `Tk()` in your code - and stopped looking at it; nothing can possibly work right in such a situation. You have to use `Toplevel()` to create additional windows. – jasonharper May 22 '20 at 15:55
  • @jasonharper um ok, I am new to tkinter, but could you tell me how to do this? maybe copy and edit my code to show me how? – Tf0R24 May 22 '20 at 15:57
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/a/48045508/7414759) and [When to use the Toplevel Widget](http://effbot.org/tkinterbook/toplevel.htm) – stovfl May 22 '20 at 16:21

1 Answers1

1

Here. I completely reworked your code, and it works(as far as I can tell).

I completely removed the textvariable and StringVar(). They aren't needed. The reason your code wasn't working is because you check the name of the file "string", but you need to add ".txt" in order to get a perfect equality.

Code:

import tkinter
from tkinter import*

from tkinter import messagebox

username = ("Tom")
password = ("test") 
usernameguess1 = ("")
passwordguess1 = ("")



def loginpage():
    #Gui Formatting
    root = tkinter.Toplevel()
    #root.resizable(width=FALSE, height=FALSE)
    root.title("HasherTest_V0.1 Login")
    root.geometry("300x150")

    #Username and password boxes
    usernametext = tkinter.Label(root, text="Username:")
    usernameguess = tkinter.Entry(root)
    passwordtext = tkinter.Label(root, text="Password:")
    passwordguess = tkinter.Entry(root, show="*")

    usernameguess1 = usernameguess
    def trylogin():
        print ("Trying to login...")
        if usernameguess.get() == username:
           print ("Complete sucsessfull!")
           messagebox.showinfo("Sucess ", "Successfully logged in.")
        else:
            print ("Error: (Incorrect value entered)")
            messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")
        #login button

    attemptlogin = tkinter.Button(root, text="Login", command=trylogin)

    usernametext.pack()
    usernameguess.pack()
    passwordtext.pack()
    passwordguess.pack()
    attemptlogin.pack()
    window.mainloop()


#Gui Formatting (again)
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1")
window.geometry("300x150")

loginbutton = tkinter.Button(window, text="Login", command=loginpage)
loginbutton.pack()

window.mainloop()

I also took the liberty to, as @stovfl mentioned, add Toplevel() instances instead of using Tk() multiple times.

Hope this helps!

10 Rep
  • 2,217
  • 7
  • 19
  • 33