0

I am trying to make a login app in python tkinter and I am not getting the output I expect.

Here is the output:

screenshot of output

I don't want the username and password strings to show until I click Register.

Here is the code that handles the window(s):

from tkinter import *

def login():
    print('Login Started')

def register():
    screen1 = Toplevel(screen)
    screen1.title('Register')
    screen1.geometry('300x250')
    
    username = StringVar()
    password = StringVar()
    
    Label(text = 'Username * ').pack()
    Entry(textvariable = username)
    Label(text = 'Password * ').pack()
    Entry(textvariable = password)

def main_screen():
    global screen
    screen = Tk()
    screen.geometry('300x250')
    screen.title('notes')
    Label(text = "Notes 1.0", bg = "grey", width = '300', height = '2', font = ("Calibri", 13)).pack()
    Label(text = '').pack()
    Button(text = 'Login', width = '30', height = '2', command = login()).pack()
    Label(text = '').pack()
    Button(text = 'Register', width = '30', height = '2', command = register()).pack()
    
    screen.mainloop()
    
main_screen()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • The `command=` option of a Button needs to be a function, such as `login`, rather than the result of calling that function immediately, which is written `login()`. – jasonharper Oct 02 '21 at 05:26
  • can you elaborate? @jasonharper – Ayden Quinn Oct 02 '21 at 05:29
  • @AydenQuinn command parameter requires a callable object. So in your case you need to remove the `()` in `command=login()` and make it `command=login`, similarly remove `()` for `register()`. – Art Oct 02 '21 at 06:05
  • 2
    Does this answer your question? [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) – Art Oct 02 '21 at 06:09

1 Answers1

0

So I debugged this code and found a few mistakes.

The first is that in command when you call the functions, you are not supposed to put brackets. Second, You did not put the attribute that says where to place the Label and Entry widgets. Third, you didn't pack a few widgets.

Here is your modified code:

from tkinter import *

def login():
    print('Login Started')

def register():
    screen1 = Toplevel()
    screen1.title('Register')
    screen1.geometry('300x250')
    
    username = StringVar()
    password = StringVar()
    
    Label(screen1, text = 'Username * ').pack()
    Entry(screen1, textvariable = username).pack()
    Label(screen1, text = 'Password * ').pack()
    Entry(screen1, textvariable = password).pack()

def main_screen():
    global screen
    screen = Tk()
    screen.geometry('300x250')
    screen.title('notes')
    Label(text = "Notes 1.0", bg = "grey", width = '300', height = '2', font = ("Calibri", 13)).pack()
    Label(text = '').pack()
    Button(text = 'Login', width = '30', height = '2', command = login).pack()
    Label(text = '').pack()
    Button(text = 'Register', width = '30', height = '2', command = register).pack()
    
    screen.mainloop()
    
main_screen()

Thank You!