2016-03-17 57 views
0

所以我有一個wxPython窗口,我做了很好的工作,直到我試圖添加一個新的複選框,爲用戶提供一個非對稱選項。wxPython複選框沒有設置值

import poser 
import random 
import wx 
import wx.py 
import wx.aui 


scene = poser.Scene() 
man = poser.WxAuiManager() 
root = man.GetManagedWindow() 

fig = scene.CurrentFigure() 
allowAsymetric = False 
paramList = [] 

lst = ["Full Body Morphs","All Head/Face", "Face Shape", "Forehead","Eyes", "Ear", "Nose", "Cheek", "Chin/Jaw", "Lips"] 


# Our dialog box 
class userInput(wx.Frame): 

    def __init__(self, parent) : 

     # Call the parent class initialisation method 
     wx.Frame.__init__(self, parent = parent, pos=wx.Point(675, 323), size=wx.Size(400, 400), style=wx.DEFAULT_FRAME_STYLE, title = "Random Hive") 

     global lst, allowAsymetric 


     # Set up the UI 
     #self.CenterOnScreen(wx.BOTH) 
     self.panel = wx.Panel(self,id=-1,size=(400,350)) 
     self.panel.SetBackgroundColour("#4B4B4B") 


     # Checklist 
     self.lb = wx.CheckListBox(self.panel, -1, (20, 20), (350,200), lst) 



     #Select All Button 
     self.SelectALL = wx.Button(self.panel,id=100,pos=(20,260),size=(-1,- 1),label="Select All") 
     self.SelectALL.SetBackgroundColour("#5D5D5D") 
     self.SelectALL.SetForegroundColour("#CBCBCB") 
     self.SelectALL.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseOver) 
     self.SelectALL.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Bind(wx.EVT_BUTTON, self.OnSelectALL, self.SelectALL) 

     #Select None Button 
     self.SelectNONE = wx.Button(self.panel,id=110,pos=(150,260),size=(-1,- 1),label="Select None") 
     self.SelectNONE.SetBackgroundColour("#5D5D5D") 
     self.SelectNONE.SetForegroundColour("#CBCBCB") 
     self.SelectNONE.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseOver) 
     self.SelectNONE.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Bind(wx.EVT_BUTTON, self.OnSelectNONE, self.SelectNONE) 

     #Inversion Button 
     self.SelectINVERT = wx.Button(self.panel,id=120,pos=(275,260),size=(- 1,-1),label="Invert Selection") 
     self.SelectINVERT.SetBackgroundColour("#5D5D5D") 
     self.SelectINVERT.SetForegroundColour("#CBCBCB") 
     self.SelectINVERT.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseOver) 
     self.SelectINVERT.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Bind(wx.EVT_BUTTON, self.OnSelectINVERT, self.SelectINVERT) 

     # Cancel button 
     self.Cancel = wx.Button(self.panel,id=140,pos=(20,310),size=(-1,-1),label="Close") 
     self.Cancel.SetBackgroundColour("#5D5D5D") 
     self.Cancel.SetForegroundColour("#CBCBCB") 
     self.Cancel.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseOver) 
     self.Cancel.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Cancel.Bind(wx.EVT_BUTTON, self.OnSelectCancel, self.Cancel) 

     # Ok button 
     self.OK = wx.Button(self.panel,id=150, pos=(275,310),size=(-1,-1),label="Apply") 
     self.OK.SetDefault() 
     self.OK.SetBackgroundColour("#5D5D5D") 
     self.OK.SetForegroundColour("#CBCBCB") 
     self.OK.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseOver) 
     self.OK.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Bind(wx.EVT_BUTTON, self.OnSelectOK, self.OK) 

     #Undoe Button 
     self.Undo = wx.Button(self.panel, id = 130, pos = (150,310), size= (-1,-1), label = "Undo Morphs") 
     self.Undo.SetBackgroundColour("#5D5D5D") 
     self.Undo.SetForegroundColour("#CBCBCB") 
     self.Undo.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseOver) 
     self.Undo.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Bind(wx.EVT_BUTTON, self.OnUndo, self.Undo) 

     #static text 
     self.infoLable = wx.StaticText(self.panel, pos = (20,230),label = 'Enter Morph Value') 
     self.morphNumber = wx.TextCtrl(self.panel, pos = (150,230)) 

     self.asym = wx.CheckBox(self.panel, label='Asymetric', pos=(275, 230)) 
     #asym.SetValue(False) 

     #asym.Bind(wx.EVT_CHECKBOX, self.AsymetricOn) 
     self.Bind(wx.EVT_CHECKBOX, self.AsymetricOn, self.asym) 


    def AsymetricOn(self, event): 
     sender = event.GetEventObject() 
     if sender.GetValue() == True: 
      allowAsymetric = True 
     else: 
      allowAsymetric = False 
     print allowAsymetric 

    def OnSelectALL(self,event): 
     i=0 
     while i < len(lst): 
      self.lb.Check(i,True) 
      i=i+1 

    def OnSelectNONE(self,event): 
     i=0 
     while i < len(lst): 
      self.lb.Check(i,False) 
      i=i+1 

    def OnSelectINVERT(self,event): 
     i=0 
     while i < len(lst): 
      if self.lb.IsChecked(i) == True: 
       action = False 
       self.lb.Check(i,action) 
       i=i +1 
       continue 
      elif self.lb.IsChecked(i) == False: 
       action = True 
       self.lb.Check(i,action) 
       i=i+1 
       continue 
      i=i+1 

    def OnSelectOK(self,event): 
     fig.Memorize() 
     smn = self.morphNumber.GetString(0,self.morphNumber.GetLineLength(0))  
     try: 
      mN = float(smn) 
      print "before function choice asymetric is ", allowAsymetric 
      functionChoice(self.lb.GetCheckedStrings(), mN) 
      print "after is it ", allowAsymetric 
     except: 
      print 'Enter a morph value' 


    def OnUndo(self,event): 
     fig.Reset() 
     scene.Draw() 

    def OnSelectCancel(self,event): 
     #print "Cancel clicked" 
     self.Destroy() 

    def OnMouseOver(self,event): 
     element = event.GetEventObject() 
     element.SetBackgroundColour("#737373") 
     event.Skip() 

    def OnMouseLeave(self,event): 
     element = event.GetEventObject() 
     element.SetBackgroundColour("#5D5D5D") 
     event.Skip() 

當窗口彈出,我可以檢查,並取消選中該複選框就好了,我也得到根據我點擊了什麼,像這樣打印出正確的值allowAsymetry:

假 真 假 真

但只要我點擊我確定/應用按鈕值爲false無論什麼複選框顯示:

功能之前選擇不對稱是假 在函數選擇allowAsymetric是False

我想不通爲什麼allowAsymetric的值始終設置回False。

回答

0

可悲的是你的代碼將不能獨立運行,所以我無法測試它,但它看起來好像你已經忘記了在def AsymetricOn(self, event):
申報global allowAsymetric你總是可以訪問一個全局變量,但如果你要改變它的價值,你需要將其聲明爲全局變量或者變量簡單地變成局部變量。

+0

謝謝。就是這樣,我錯過了allowAsymetric作爲全局變量的第二個聲明。 –