我在经历了6个月的python经验后开始使用tkinter,我目前正在制作一个smtp邮件发件人,唯一的问题是我网格中的小部件拒绝根据条件更新。我知道这纯粹是我的错,因为我使用的是非gui编程与事件驱动相比如何工作。
import tkinter as tk
class smtp_gui(tk.Tk):
is_logged_in = False
user_email = ''
user_passwd = ''
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_title("SMTP Mail Sender")
self.resizable(False, False)
self.iconbitmap('at.ico')
container = tk.Frame(self)
btn_container = tk.Frame(self)
container.pack(side="top", padx=1, pady=1)
container.columnconfigure(1, weight=1)
btn_container.pack(side="bottom", padx=1, pady=1, fill='x')
tk.Label(container, text='From:').grid(row=0, column=0, sticky='e')
tk.Label(container, text='To:').grid(row=1, column=0, sticky='e')
tk.Label(container, text='Subject:').grid(row=2, column=0, sticky='e')
if self.logged_in():
self.mailabel = tk.Label(container, bd=1, text=smtp_gui.user_email, relief='sunken').grid(row=0, column=1, pady=3, padx=2, sticky='we')
else:
self.login_btn = tk.Button(container, text="login before sending", command=self.get_login)
self.login_btn.grid(row=0, column=1, sticky="we", pady=3, padx=2)
self.To = tk.Entry(container)
self.To.grid(row=1, column=1, sticky="we", pady=3, padx=2)
self.Subject = tk.Entry(container)
self.Subject.grid(row=2, column=1, sticky="we", pady=2, padx=2)
self.Msg = tk.Text(container, width=40, height=5)
self.Msg.grid(columnspan=2, padx=3, pady=3)
send_btn = tk.Button(btn_container, text="Send", command=self.send)
send_btn.pack(side='right', padx=2, pady=1)
quit_btn = tk.Button(btn_container, text="Quit", command=self.quit)
quit_btn.pack(side='right', padx=2, pady=1)
def send(self):
print(self.Msg.get("0.0", "end"))
def logged_in(self):
return smtp_gui.is_logged_in
def login(self):
pass
def get_login(self):
login = tk.Toplevel(self)
login.grab_set()
login.wm_title("Email login")
login.resizable(False, False)
login.iconbitmap('login.ico')
container = tk.Frame(login)
btn_container = tk.Frame(login)
container.pack(padx=1, pady=1)
btn_container.pack(side="bottom", padx=1, pady=1, fill='x')
tk.Label(container, text="Gmail address:").grid(row=0, column=0)
tk.Label(container, text="Password:").grid(row=1, column=0)
login.Address = tk.Entry(container)
login.Address.grid(row=0, column=1, sticky="we", pady=3, padx=2)
login.Address.insert(0, "your address")
login.Passwd = tk.Entry(container, show="*")
login.Passwd.grid(row=1, column=1, sticky="we", pady=3, padx=2)
login_btn = tk.Button(btn_container, text="Login", command=None)
login_btn.pack(side='right', padx=2, pady=3)
storno_btn = tk.Button(btn_container, text="Storno", command=login.destroy)
storno_btn.pack(side='right', padx=2, pady=3)
root = smtp_gui()
root.mainloop()
所以..我想必须有一些方法来根据条件或东西更新网格内容。我希望窗口显示登录按钮,如果用户在登录时没有登录或显示他的地址,则按钮和标签位于网格中的相同位置,如代码中所示。
感谢您的回答!
编辑 - 现在可以重现此问题。