2

I am making an emial client and i would like the two buttons (send mail and reset) to be next to eachother horizontally (not vertically). Thanks in advance (my email and password have been replaced with asterix's for security purposes)

Code:

from tkinter import *
import tkinter as tk
from email.message import EmailMessage
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

window=Tk()
window.title('Email Client')
window.geometry('450x370')
window.iconbitmap('D:\PYTHON\IMAGES\icon_for_email_clien_Jy16u.ico')


textbox2=Text(window,width=50,height=10,bg='light grey')
label2=Label(window,text='Message')
textbox1=Text(window,width=50,height=1,bg='light grey')
textbox3=Text(window,width=50,height=1,bg='light grey')
label4=Label(window,text='Directory of File Attachment')
label1=Label(window,text='Subject')
label3=Label(window,text='')

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():
        gd = textbox3.get('1.0','end-1c')
        dialog = textbox2.get('1.0','end-1c')
        subject = textbox1.get('1.0','end-1c')
        body = dialog
        try:
            fromaddr = "********************"
            toaddr = "*******************"

            msg = MIMEMultipart()
            msg['From'] = fromaddr
            msg['To'] = toaddr
            msg['Subject'] = subject
            body = dialog

            msg.attach(MIMEText(body, 'plain'))

            filename = "Homework Attachment"
            attachment = open(gd, "rb")
            p = MIMEBase('application', 'octet-stream')
            p.set_payload((attachment).read())
            encoders.encode_base64(p)
            p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
            msg.attach(p)
            s = smtplib.SMTP('smtp.gmail.com', 587)
            s.starttls()
            s.login(fromaddr, "*************")
            text = msg.as_string()
            s.sendmail(fromaddr, toaddr, text)

            s.quit()
        except:
            email_alert(subject, dialog, "************************")

def Reset():
    textbox1.delete('1.0', 'end-1c')
    textbox2.delete('1.0', 'end-1c')
    textbox3.delete('1.0', 'end-1c')

label1.pack()
textbox1.pack()
label4.pack()
textbox3.pack()
label2.pack()
textbox2.pack()
label3.pack()
button1=Button(window,text='Send Email',width=15,height=1, command=Send)
button1.pack()
button2=Button(window,text='Reset',width=15,height=1, command=Reset)
button2.pack(pady = 10)

window.mainloop()

image:

The two buttons below (called send email and reset), i would like them to be horizontally next to eachother to make the GUI look a bit more appealing. However I am having trouble doing this and i have not found a suitable answer to my query. Any help is greatly appreciated :D

enter image description here

some_user_3
  • 403
  • 1
  • 5
  • 17

2 Answers2

1

You have to use more arguments in pack to make this happen.

  • side ~ the side to pack on
  • anchor ~ where to position the widget in the packed area
  • expand ~ whether to make the cell expand to touch it's neighbors

button1=Button(window,text='Send Email',width=15,height=1, command=Send)
button1.pack(side='left', anchor='e', expand=True)
button2=Button(window,text='Reset',width=15,height=1, command=Reset)
button2.pack(side='right', anchor='w', expand=True)

enter image description here

OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
0

Here is the working code, So i have modified some of the statements in the program to use Frame and grid instead of simply pack. here is the code

...

label1.pack()
textbox1.pack()
label4.pack()
textbox3.pack()
label2.pack()
textbox2.pack()
label3.pack()

buttonframe = Frame(window)
buttonframe.pack()

button1=Button(buttonframe,text='Send Email',width=15,height=1, command=Send)
button1.grid(row=0, column=0, padx=5, pady=5)
button1.grid_rowconfigure(0, weight=1)
button2=Button(buttonframe,text='Reset',width=15,height=1, command=Reset)
button2.grid(row=0, column=1, padx=5, pady=5)

window.mainloop()

enter image description here

Vishnu Vinod
  • 603
  • 4
  • 15
  • Thanks for the answer! Unfortunately i accepted Michael Guidry's answer. However, this method also works and will aid me when using the grid method in future projects. Thanks :D – some_user_3 Aug 23 '20 at 14:05