· 8 years ago · Apr 05, 2018, 10:06 AM
1import tkinter as tk
2import smtplib
3
4TITLE_FONT = ("Helvetica", 18, "bold")
5
6class SampleApp(tk.Tk):
7
8 def __init__(self):
9 tk.Tk.__init__(self)
10
11
12 container = tk.Frame(self)
13 container.pack(side="top", fill="both", expand=True)
14 container.grid_rowconfigure(0, weight=1)
15 container.grid_columnconfigure(0, weight=1)
16
17
18
19 self.frames = {}
20 for F in (StartPage, PageOne, PageTwo):
21 frame = F(container, self)
22 self.frames[F] = frame
23
24 frame.grid(row=0, column=0, sticky="nsew")
25
26 self.show_frame(StartPage)
27
28 def show_frame(self, c):
29 frame = self.frames[c]
30 frame.tkraise()
31
32
33class StartPage(tk.Frame):
34
35 def __init__(self, parent, controller):
36 tk.Frame.__init__(self, parent)
37 label = tk.Label(self, text="PyMail",foreground = "Red", font=("Courier", 30, "bold"))
38 label.pack(side="top")
39 sublabel = tk.Label(self, text="Bringing you then the easiest way of communication",
40 font=("Courier", 15))
41 sublabel.pack()
42
43 wallpaper = tk.PhotoImage(file='Python-logo-notext.gif')
44 img = tk.Label(self, image=wallpaper)
45 img.image = wallpaper
46 img.pack(side="top", expand = True)
47
48 button1 = tk.Button(self, text="Click Here to Login to your account",fg="red",
49 command=lambda: controller.show_frame(PageOne))
50 button2 = tk.Button(self, text="Go to Page Two",
51 command=lambda: controller.show_frame(PageTwo))
52 button2.pack(side="bottom")
53 button1.pack(side="bottom")
54
55
56class PageOne(tk.Frame):
57
58 def __init__(self, parent, controller):
59 tk.Frame.__init__(self, parent)
60 self.controller=controller
61 label = tk.Label(self, text="Personal Information", font=TITLE_FONT, foreground="blue")
62 label.pack(side="top", fill="x", pady=10)
63 global optionv
64 self.optionv = tk.StringVar()
65 self.optionv.set("---Select One---")
66 optionm = tk.OptionMenu(self, self.optionv, "---Select One---", "@gmail.com", "@yahoo.com", "@hotmail.com")
67
68 t1 = tk.Label(self, text="Email Account: ")
69 self.v = tk.StringVar()
70 self.v.set("")
71 entry1 = tk.Entry(self, textvariable=self.v)
72 t2 = tk.Label(self,text="nPassword: ")
73 self.pwd = tk.StringVar()
74 self.pwd.set("")
75 entry2 = tk.Entry(self, textvariable=self.pwd)
76 entry2.config(show="*")
77 lgbutton=tk.Button(self, text="Log In", command=self.login)
78 button = tk.Button(self, text="Go to the start page",
79 command=lambda: controller.show_frame(StartPage))
80 #final = tk.Label(self, textvariable=self.v)
81 #finalpwd = tk.Label(self, textvariable=self.pwd)
82
83 t1.pack()
84 entry1.pack()
85 optionm.pack()
86 t2.pack()
87 entry2.pack()
88 #final.pack()
89 #finalpwd.pack()
90 lgbutton.pack()
91 button.pack(side="bottom")
92
93 def login(self):
94 value = tk.Label(self, text="Invalid username / password", font=("Courier", 15, "bold"), foreground="red")
95 success = tk.Label(self, text="Login was Successful n (Click ""Continue"" to compose email)", font=("Courier", 15, "bold"), foreground="blue")
96 cbutton = tk.Button(self, text="Continue", command=lambda: self.controller.show_frame(PageTwo))
97 status = tk.Label(self, text="Please select your email domain", foreground="red")
98 if self.optionv.get() == "@gmail.com":
99 try:
100 global server
101 server = smtplib.SMTP("smtp.gmail.com", 587)
102 server.ehlo()
103 server.starttls()
104 server.login(self.v.get()+self.optionv.get(), self.pwd.get())
105 success.pack()
106 cbutton.pack(side="bottom")
107 except:
108 value.pack()
109
110
111 elif self.optionv.get() == "@yahoo.com":
112 try:
113
114 server = smtplib.SMTP("smtp.yahoo.com", 587)
115 server.ehlo()
116 server.starttls()
117 server.login(self.v.get()+self.optionv.get(), self.pwd.get())
118 success.pack()
119 cbutton.pack(side="bottom")
120 except:
121 value.pack()
122
123 elif self.optionv.get() == "@hotmail.com":
124 try:
125
126 server = smtplib.SMTP("smtp.live.com", 587)
127 server.ehlo()
128 server.starttls()
129 server.login(self.v.get()+self.optionv.get(), self.pwd.get())
130 success.pack()
131 cbutton.pack(side="bottom")
132 except:
133 value.pack()
134
135 else:
136 status.pack()
137
138
139
140
141class PageTwo(tk.Frame):
142
143 def __init__(self, parent, controller):
144 tk.Frame.__init__(self, parent)
145 label = tk.Label(self, text="Compose Mail", font=TITLE_FONT, foreground="green")
146 label.pack(side="top", fill="x", pady=10)
147
148 self.reciever = tk.StringVar()
149 self.reciever.set("")
150 senderl = tk.Label(self, text="Send to: ")
151 rmail = tk.Entry(self, textvariable=self.reciever)
152
153 self.senderoption = tk.StringVar()
154 self.senderoption.set("---Select One---")
155 senderdomain = tk.OptionMenu(self, self.senderoption, "---Select One---", "@gmail.com", "@hotmail.com", "@yahoo.com")
156
157 self.mail = tk.StringVar()
158 self.mail.set("")
159 self.textw = tk.Entry(self, textvariable=self.mail)
160
161 button = tk.Button(self, text="Go to the start page",
162 command=lambda: controller.show_frame(StartPage))
163
164 sendbutton = tk.Button(self, text = "Send Mail", command=self.sendmail)
165
166 senderl.pack(side="top", anchor="w")
167 rmail.pack(side="top", anchor="nw")
168 senderdomain.pack(side="top", anchor="nw")
169 self.textw.pack(fill="both")
170 button.pack(side="bottom")
171 sendbutton.pack(side="bottom")
172
173 def sendmail(self):
174 sent = tk.Label(self, text="Email has been sent")
175 if self.senderoption.get() == "@gmail.com":
176 try:
177 server.sendmail(self.v.get()+self.optionv.get(), self.reciever.get()+self.senderoption.get(), "YES")
178 print("Success")
179 sent.pack()
180 except:
181 print("Unsuccesful")
182 print(PageOne.self.v.get())
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198if __name__ == "__main__":
199 app = SampleApp()
200 app.title("PyMail")
201 app.geometry("400x400")
202 app.mainloop()
203
204class PageTwo(tk.Frame):
205 def __init__(self, parent, controller):
206 ...
207 self.controller=controller
208 ...
209
210class SampleApp(tk.Tk):
211 ...
212 def get_page(self, page_class):
213 return self.frames[page_class]
214 ...
215
216page1 = self.controller.get_page(PageOne)
217page1.v.set("Hello, world")
218
219class SampleApp(tk.Tk):
220 def __init__(self, *args, **kwargs):
221 tk.Tk.__init__(self, *args, **kwargs)
222 self.shared_data = {
223 "username": tk.StringVar(),
224 "password": tk.StringVar(),
225 ...
226 )
227
228entry1 = tk.Entry(self, textvariable=self.controller.shared_data["username"])
229...
230username = self.controller.shared_data["username"].get()
231
232import tkinter as tk
233from tkinter import StringVar
234LARGE_FONT = ("Verdana", 12)
235
236class Club(tk.Tk):
237
238 def get_page(self, page_class):
239 return self.frames[page_class]
240
241 def __init__(self, *args, **kwargs):
242 tk.Tk.__init__(self, *args, **kwargs)
243 container = tk.Frame(self)
244
245 container.pack(side="top", fill="both", expand=True)
246
247 container.grid_rowconfigure(0, weight=1)
248 container.grid_columnconfigure(0, weight=1)
249
250 self.shared_data = {
251 "username": tk.StringVar(),
252 "password": tk.StringVar(),
253 }
254
255 self.frames = {}
256
257 for F in (Terminal, newUser, newUserSubmitButton, delUser):
258 frame = F(container, self)
259
260 self.frames[F] = frame
261
262 frame.grid(row=0, column=0, sticky="nsew")
263
264 self.show_frame(Terminal)
265
266 def show_frame(self, cont):
267 frame = self.frames[cont]
268 frame.tkraise()
269
270
271class Terminal(tk.Frame):
272
273 def __init__(self, parent, controller):
274 tk.Frame.__init__(self, parent)
275 label = tk.Label(self, text="Welcome to the club terminal. Click the options below", font=LARGE_FONT)
276 label.grid(columnspan=3)
277
278 button = tk.Button(self, text="Visit new User",
279 command=lambda: controller.show_frame(newUser))
280 button.grid(row=1, column=0)
281
282 button2 = tk.Button(self, text="Visit del User",
283 command=lambda: controller.show_frame(delUser))
284 button2.grid(row=1, column=1)
285
286
287class newUser(tk.Frame):
288
289 def __init__(self, parent, controller):
290
291 def submitButton():
292 username = self.controller.shared_data["username"].get()
293 print(username)
294 newUserSubmitButton(self, parent, controller)
295
296 tk.Frame.__init__(self, parent)
297 welcomelabel = tk.Label(self, text="Add New User/s", font=LARGE_FONT)
298 welcomelabel.grid(columnspan=2, sticky="ew")
299
300 userNameLabel = tk.Label(self, text="Username")
301 userNameLabel.grid(row=1, column=0, sticky="e")
302 userNameEntry = tk.Entry(self, textvariable=self.controller.shared_data["username"])
303 userNameEntry.grid(row=1, column=1)
304
305 userMemTypeLabel = tk.Label(self, text="Membership Type")
306 userMemTypeLabel.grid(row=2, column=0, sticky="e")
307 variable = StringVar(self)
308 variable.set("Full")
309 userMemTypeMenu = tk.OptionMenu(self, variable, "Full", "Half")
310 userMemTypeMenu.grid(row=2, column=1)
311
312 userMemYearsLabel = tk.Label(self, text="Years that member is in the club")
313 userMemYearsLabel.grid(row=3, column=0, sticky="e")
314 userMemYearsEntry = tk.Entry(self)
315 userMemYearsEntry.grid(row=3, column=1)
316
317 newusersubmitbutton = tk.Button(self, text="submit", command=submitButton)
318 newusersubmitbutton.grid(columnspan=2)
319class newUserSubmitButton(tk.Frame):
320
321 def __init__(self, parent, controller):
322 tk.Frame.__init__(self, parent)
323 self.controller = controller
324 # username = newUser.submitButton()
325 print(username)
326
327
328class delUser(tk.Frame):
329
330 def __init__(self, parent, controller):
331 tk.Frame.__init__(self, parent)
332 label = tk.Label(self, text="del User!!!", font=LARGE_FONT)
333 label.pack(pady=10, padx=10)
334
335 button1 = tk.Button(self, text="Back to Home",
336 command=lambda: controller.show_frame(Terminal))
337 button1.pack()
338
339 button2 = tk.Button(self, text="new User",
340 command=lambda: controller.show_frame(newUser))
341 button2.pack()
342
343app = Club()
344app.title("Club Terminal")
345app.iconbitmap("table.ico")
346app.mainloop()
347
348class firstClass():
349 global my_var_first
350 my_var_first = "first variable"
351
352print(my_var_first) # This will work, because the var is in the global frame
353
354class secondClass():
355 my_var_second = "second variable"
356 print(my_var_first) # This will work, as the var is in the global frame and not defined in the class
357
358print(my_var_second) # This won't work, because there is no my_var_second in the global frame
359
360x = 5
361def print_variable():
362 x = 3
363 print(x)
364print(x)
365print_variable()
366
367# OUTPUT:
368# 5
369# 3