繁体   English   中英

如何在python tkinter中使两个按钮彼此相邻(并居中)?

[英]How to make two buttons be next to eachother (and centered) in python tkinter?

我正在制作一个 emial 客户端,我希望两个按钮(发送邮件和重置)水平(而不是垂直)彼此相邻。 提前致谢(为了安全起见,我的电子邮件和密码已被替换为 asterix)

代码:

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()

图片:

下面的两个按钮(称为发送电子邮件和重置),我希望它们水平相邻,以使 GUI 看起来更吸引人。 但是,我在执行此操作时遇到了麻烦,而且我还没有找到适合我的查询的答案。 非常感谢任何帮助:D

在此处输入图片说明

您必须在 pack 中使用更多参数才能实现这一点。

  • 侧面 ~ 包装的侧面
  • 锚点 ~ 在打包区域中放置小部件的位置
  • expand ~ 是否让单元格扩展到接触它的邻居

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)

在此处输入图片说明

这是工作代码,所以我修改了程序中的一些语句以使用框架和网格而不是简单地打包。 这是代码

...

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()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM