Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:02:11+00:00 2026-05-27T22:02:11+00:00

I made a function in my wxpython app that pop up a passwordbox. The

  • 0

I made a function in my wxpython app that pop up a passwordbox. The code, that’s present in dialogs.py, looks like this:

def password_dialog(self, password):
    # Only ask for password if it actually exist
    if password == 'False':
        return True

    question = 'Put in password:'
    dialog = wx.PasswordEntryDialog(self, question, 'Password...')
    if dialog.ShowModal() == wx.ID_OK:
        if dialog.GetValue() == password:
            dialog.Destroy()
            return True
        else:
            dialog.Destroy()
            __wrong_pass()
            raise WrongPassword
    else:
        dialog.Destroy()
        raise CancelDialog

The exceptions is in the same file:

class WrongPassword(Exception):
    pass

class CancelDialog(Exception):
    pass   

In my main program I then have some methods that look something like this:

def on_sort_songs(self, event): 
    """Renumbering the database and sort in artist and title order"""
    # Check for password first
    try:
        dialogs.password_dialog(self, opts.generic['password'])
    except dialogs.CancelDialog:
        return
    except dialogs.WrongPassword:
        return

    # Sort database and repopulate GUI
    self.jbox.sort_songs()
    self.populate_songlist()

It work ok. But it don’t seems like a very good and pythonic way to handle password dialogs. Or is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:02:12+00:00Added an answer on May 27, 2026 at 10:02 pm

    I dont think your dialog function should be raising exceptions in this case. Just have it return True or False depending on whether the validation passes or not. Then all you need to do is:

    validated = dialogs.password_dialog(self, opts.generic['password'])
    if validated:
        print "Yay"
    else:
        print "Boo"
    

    Exceptions would only be necessary for other random failure cases that you want to distinguish, such as “Authentication Server is Down”

    Another reason I think its good to return True or False in this case is because then you can use modular authentication methods that can be swapped out. Such as how django uses a single is_authenticated() method that returns a boolean. The end use only needs to worry about whether its authenticated or not. Not what various exceptions it raises specifically, like a dialog being closed. Some cases might not even use a dialog..maybe a command line, or a web interface ,etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made some function that generates an email template. Code it generates is pure
Hi have made this function which is made to replicate an error that I
I have made this function, that seems to work fine, but it return #
Hi I have made this function that takes a table and prepare the label
I made a function like this (no need to write the whole function here)
I made a function like this: bool IsSameString(char* p1, char* p2) { return 0
Anyone got a ready made function that will take an XML string and return
I made this function to verify a user's twitter credentials. Its running on two
I have made a function to cound the weeks in a year, and that
I made this bookmarklet: javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})() Formatted code: // Add in jQuery var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.