[Tkinter-discuss] Re-sudo password

Kevin Walzer kw at codebykevin.com
Sat Nov 3 14:31:23 CET 2007


Rajeev Nair wrote:
> hi
> 
> I have a function defined using os.system("mount ....."). as a
> callback to a button.
> 
> On clicking this button the system asks for password in terminal. How
> do i enter this password in a tk window so that the system reads it
> ?.Iam using the entry widget to do this.
> 

> 
Here's code I use for this:

     #get user password to run commands as root
     def authorizeCommand(self, cmd):

         self.passtext = StringVar()
         self.password = Toplevel(self)
         self.password.title('Password Required')

         self.passtop = Tkinter.Frame(self.password, padding=5)
         self.passtop.pack(side=TOP, fill=BOTH, expand=YES)

         self.passlabel =  Tkinter.Label(self.passtop, text='You must 
enter your password.')
         self.passlabel.pack(side=TOP, fill=BOTH, expand=YES)
         self.passlabel.image=self.lock

         self.passentry = Tkinter.Entry(self.passtop, show='*', 
textvariable=self.passtext)
         self.passentry.pack(side=BOTTOM, fill=BOTH, expand=YES)
         self.passentry.bind('<Return>', (lambda event: 
self.runCommand(cmd)))

         self.passbottom = Tkinter.Frame(self.password, padding=5)
         self.passbottom.pack(side=BOTTOM, fill=BOTH, expand=YES)

         self.bottomupper = Tkinter.Frame(self.passbottom)
         self.bottomupper.pack(side=TOP, fill=BOTH, expand=YES)

         self.bottomlower = Tkinter.Frame(self.passbottom, padding = 5)
         self.bottomlower.pack(side=BOTTOM, fill=BOTH, expand=NO)

         self.passinstall = Tkinter.Button(self.bottomlower, text='OK', 
default=ACTIVE, command=lambda: self.runCommand(cmd))
         self.passinstall.grid(row = 1, column = 1, sticky = NSEW)

         self.passcancel = Tkinter.Button(self.bottomlower, 
text='Cancel', command=self.password.destroy)
         self.passcancel.grid(row=1, column = 0, sticky = NSEW)

  #run command with root privileges
     def runCommand(self, cmd):

        self.passtext=self.passtext.get()
        self.password.destroy()
        cmd()

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com


More information about the Tkinter-discuss mailing list