wxPython复选框没有设置值

时间:2016-03-17 20:30:03

标签: checkbox 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的正确值:

假 真正 假 真

但是只要我点击我的确定/应用按钮,无论复选框显示什么,该值都为假:

在函数选择之前,asymetric为False 在函数选择allowAsymetric中为False

我无法弄清楚为什么allowAsymetric的值会一直设置为False。

1 个答案:

答案 0 :(得分:0)

可悲的是,您的代码不会独立运行,因此我无法对其进行测试,但看起来好像您忘记在global allowAsymetric中声明def AsymetricOn(self, event):了 您始终可以访问全局变量,但如果要更改其值,则需要将其声明为全局变量,或者变量只是变为局部变量。