Python从不同的文件访问全局变量

时间:2014-04-01 15:53:09

标签: python wxpython

以下是我的密码检查程序。如何将标记设为全局,以便我可以从其他文件访问它? (即使用"来自this_file_name导入标志"在另一个文件中)

另外,如何屏蔽输入?

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

1 个答案:

答案 0 :(得分:1)

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

您可能还想查看pubsub上的wxPython wiki page