简体   繁体   中英

Python accessing global variable from a different file

Below is my password checker program. How do I make flag a global so that I could access it from another file? (ie by using "from this_file_name import flag" in another file)

Also, how can I mask the input?

import wx, os, re    

class MyClass(wx.Frame):
    def __init__(self, parent=None, id=-1):
        box1 = wx.TextEntryDialog(None, 'Enter a password:')
        if box1.ShowModal() == wx.ID_OK:
                answer1 = box1.GetValue()
                if re.search(r'\d', answer1) and re.search(r'[A-Z]', answer1) and re.search(r'[a-z]', answer1) and len(answer1) > 6:
                    box2 = wx.MessageBox('Your Password is Strong.')
                    global flag
                    flag = 0
                else:
                    box2 = wx.MessageBox('Your Password is weak. It must contain atleast one uppercase, one lowercase letter and one '
                                         'digit.')
                    global flag
                    flag = 1
if __name__=='__main__':
    app = wx.App(0)
    frame = MyClass()
    frame.Show()
    app.MainLoop()

You'll probably want to use pubsub to pass the information between the two classes in the two modules (files). Pubsub is included with wxPython. The idea is to subscribe to a topic and then publish updates to that topic. The subscriber (or listener) will then call a function to update the GUI. There are a couple of tutorials out for pubsub:

You might also want to check out the wxPython wiki page on pubsub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM