Skip to content

4. The Login Window

Vismaya Atreya edited this page Aug 11, 2020 · 1 revision

Now that both the functions are defined, it is time to create the login window.

login = tk.Tk()
login.title('Login')
username_label = tk.Label(login, text = 'Your username:')
username_entry = tk.Entry(login)
password_label = tk.Label(login, text = 'Your password:')
password_entry = tk.Entry(login)
server_label = tk.Label(login, text = 'Your SMTP server:')
list_of_servers = ['Gmail', 'Outlook', 'Office 365', 'Yahoo! Mail', 'Yahoo! Mail Plus', 'iCloud']
server_name = tk.StringVar(login)
servers_menu = tk.OptionMenu(login, server_name, *list_of_servers)
login_button = tk.Button(login, text = 'Login', command = signin)
username_label.pack()
username_entry.pack()
password_label.pack()
password_entry.pack()
server_label.pack()
server_name.set(list_of_servers[0])
servers_menu.config(width=40, font=('Helvetica', 12))
servers_menu.pack()
login_button.pack()
login.mainloop()

Let us break this down into two parts

login = tk.Tk()
login.title('Login')
username_label = tk.Label(login, text = 'Your username:')
username_entry = tk.Entry(login)
password_label = tk.Label(login, text = 'Your password:')
password_entry = tk.Entry(login)
server_label = tk.Label(login, text = 'Your SMTP server:')
list_of_servers = ['Gmail', 'Outlook', 'Office 365', 'Yahoo! Mail', 'Yahoo! Mail Plus', 'iCloud']
server_name = tk.StringVar(login)
servers_menu = tk.OptionMenu(login, server_name, *list_of_servers)
login_button = tk.Button(login, text = 'Login', command = signin)

The first line creates a tkinter window called login

The second line changes it's title to 'Login'

The third line saves a tkinter label in login with its text as 'Your username:' as a variable called username_label

The fourth line saves a tkinter entry in login as a variable called username_entry

The same is done in the next two lines for the password.

The seventh line saves a tkinter label in login with its text as 'Your SMTP server' as a variable called server_label

In the eighth line, a list with all the smtp servers is created

In the ninth line, a tkinter string-variable is created in for login window

In the tenth line, a tkinter OptionMenu is saved in a variable called servers_menu. An OptionMenu is a dropdown menu widget of tkinter. The second argument is the variable in which the chosen option should be saved, and the third argument is the list of options, in this case it is list_of_servers.

In the last line, a button is saved in login, with its text as 'Login' and command as signin, which we defined earlier.

username_label.pack()
username_entry.pack()
password_label.pack()
password_entry.pack()
server_label.pack()
server_name.set(list_of_servers[0])
servers_menu.config(width=40, font=('Helvetica', 12))
servers_menu.pack()
login_button.pack()
login.mainloop()

Here, from the first to fifth line, the widgets are shown with their .pack() attribute.

In the sixth line, the server_name StringVar is set to index 0 of the list_of_servers list.

In the seventh line, the server_menu OptionMenu is configured with the width of 40 and the font is set to Helevectica, size 12.

In the eighth and ninth line, the remaining widgets are shown.

In the last line, the login window is shown with the .mainloop() attribute.

Run your code

The README.md contains all instructions to run the code.

Have any questions

If you have any questions, you can create an issue and I will get back to you as fast as possible.

If you liked this tutorial, spread the word.

Thank you!

Clone this wiki locally