TypeError: get() missing 1 required positional argument: 'index1'

Viewed 8273

I am trying to make a super simple email client. This will enable me to send emails from school to my home computer because they have outlook and gmail blocked. When i try to run it, it says: TypeError: get() missing 1 required positional argument: 'index1' I have replaced my email and password with asterix's for security purposes. Thanks in advance :)

Heres my code:

from tkinter import *
import tkinter as tk
from email.message import EmailMessage
import smtplib

window=Tk()
window.title('Email Client')
window.geometry('200x275')

textbox2=Text(window,width=20,height=10,bg='light grey')
label2=Label(window,text='Message')
textbox1=Text(window,width=20,height=1,bg='light grey')
label1=Label(window,text='Subject')

def email_alert(subject, body, to):
    msg = EmailMessage()
    msg.set_content(body)
    msg['subject'] = subject
    msg['to'] = to

    user = "*******************"
    msg['from'] = user
    password = "*************"

    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(user, password)
    server.send_message(msg)

    server.quit()

if __name__ == '__main__':
    def Send():
        dialog = textbox1.get()
        subject = textbox2.get()
        email_alert(subject, dialog, "******************")

label1.pack()
textbox1.pack()
label2.pack()
textbox2.pack()

button1=Button(window,text='Send Email',width=10,height=1, command=Send)
button1.pack()

window.mainloop()
3 Answers

On your send() function, you need to add get() parameters.

dialog = textbox1.get("1.0",'end-1c')
subject = textbox2.get("1.0",'end-1c')

The first part, "1.0" means that the input should be read from line one, character zero.

The end-1c is divided in 2 parts:

  1. end: Read until the end of the text.
  2. 1c: Remove 1 character starting from the end.

It deletes the last character to remove that last \n so your e-mail doesn't end with an extra line.

the error happened because the .get method requires at least 1 parameter. To get the contents, you have to use .get("1.0",END)

try

from tkinter import *
import tkinter as tk
from email.message import EmailMessage
import smtplib

window=Tk()
window.title('Email Client')
window.geometry('200x275')

textbox2=Text(window,width=20,height=10,bg='light grey')
label2=Label(window,text='Message')
textbox1=Text(window,width=20,height=1,bg='light grey')
label1=Label(window,text='Subject')

def email_alert(subject, body, to):
    msg = EmailMessage()
    msg.set_content(body)
    msg['subject'] = subject
    msg['to'] = to

    user = "*******************"
    msg['from'] = user
    password = "*************"

    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(user, password)
    server.send_message(msg)

    server.quit()

if __name__ == '__main__':
    def Send():
        dialog = textbox1.get("1.0",END)
        subject = textbox2.get("1.0",END)
        email_alert(subject, dialog, "******************")

label1.pack()
textbox1.pack()
label2.pack()
textbox2.pack()

button1=Button(window,text='Send Email',width=10,height=1, command=Send)
button1.pack()

window.mainloop()
Related