Thursday, September 3, 2015

Python: Tkinter (8)

Abstract: a GUI of password window

The GUI:


The script:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 30 09:22:18 2015

@author: yuan
"""


from Tkinter import *
import tkMessageBox


#class   
class MyDialog:
    def __init__(self, old_password):
        self.root = Tk()
        self.root.title('Change password')
        #add display
        self.root=self.display_body(self.root, row=0)
        #add buttons
        self.buttons(self.root,row=4)
        self.old_p=old_password
        self.new_p=old_password
        self.root.mainloop()
       
    def display_body(self, win, row=0):
        #entry 1
        Label(win, text="Old password:").grid(row=row,column=0,sticky=W)
        self.e1=StringVar()
        Entry(win, width=16,show='*',textvariable=self.e1).grid(row=row, column=1)       
        #entry 2
        row+=1
        Label(win, text="New password:").grid(row=row,column=0,sticky=W)
        self.e2 = StringVar()
        Entry(win, width=16,show='*',textvariable=self.e2).grid(row=row, column=1)
        Label(win, text="(At least 8 characters)").grid(row=row,column=2,sticky=W)
        #entry 3
        row+=1
        Label(win, text="New password:").grid(row=row,column=0,sticky=W)
        self.e3 = StringVar()
        self.entry3=Entry(win, width=16,show='*',textvariable=self.e3)
        self.entry3.grid(row=row, column=1)       
        Label(win, text="Enter passward again").grid(row=row,column=2,sticky=W)
       
        #checkbuttons
        row+=1
        self.var=IntVar()
        self.var.set(1)#default show password
        self.cb = Checkbutton(win, text="Show password", variable=self.var, onvalue=1,
              command=self.show_password)
        self.cb.grid(row=row, column=0, sticky=W)
        return win
       
    def buttons(self, win, row=0):
        print self.e1, self.e2, self.e3
        self.b1=Button(win, text='Clear',command=self.clear_entry )
        self.b1.grid(row=row,column=0, sticky=W)
        self.b2=Button(win, text='Save',command=self.save_entry )
        self.b2.grid(row=row,column=1, sticky=W)
        self.b3=Button(win, text='Close',command=win.quit)
        self.b3.grid(row=row,column=2, sticky=W)
        return win
       
    def show_password(self):
        #self.e3.set(self.e3.get())
        print self.var.get()
       
    def clear_entry(self):
        self.e1.set('')
        self.e2.set('')
        self.e3.set('')
       
    def save_entry(self):
        first_p = self.e2.get()
        second_p = self.e3.get()
        print'==',  self.var.get()
        if first_p==second_p:
            print first_p, second_p
            if second_p==self.old_p:
                tkMessageBox.showerror('Error',
                       'Old and new password shouldnot be the same')
            elif len(second_p)<8:
                tkMessageBox.showerror('Error',
                     'Length of new password should be more than 8 characters')
            else:
                self.new_p=second_p
        else:
            tkMessageBox.showerror('Error','New passwords do not match')
       
    def export_password(self):
        return self.new_p






if __name__=="__main__":

   
    p=MyDialog('123456')
    print 'Password:',p.export_password()
   
    print 'ok'

No comments:

Post a Comment