2014-04-01 48 views
0

以下是我的密码检查程序。如何使标记为为全球性的,以便我可以从另一个文件访问它? (即通过在另一个文件中使用“from this_file_name导入标志”)Python从不同的文件访问全局变量

另外,如何屏蔽输入?

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() 
+0

参见:HTTP:// stackoverflow.com/questions/21891411/global-variable-between-frames-in-wxpython –

回答

1

您可能想要使用pubsub在两个模块(文件)中的两个类之间传递信息。 Pubsub包含在wxPython中。这个想法是订阅一个主题,然后发布该主题的更新。用户(或监听者)然后将调用一个函数来更新GUI。有一对夫妇的教程出来发布订阅:

您可能还需要检查出的wxPython wiki page上发布订阅