2014-04-01 55 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上發佈訂閱