[英]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 中使用更多參數才能實現這一點。
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.