tags:

views:

24

answers:

2

When I am writing my code, I find the following situation many times:

def Mwindow_stayontop(self, event):
    if CFG["AlwOnTop"] == 1:
        self.SetWindowStyle(wx.DEFAULT_FRAME_STYLE)
        CFG["AlwOnTop"] = 0
    else:
        self.SetWindowStyle(wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP)
        CFG["AlwOnTop"] = 1

Can anybody think of a simpler way of doing this? I tried to use SIN and COS to alternate the value between 0 and 1, but couldn't.

Ideas?

+2  A: 
CFG["AlwOnTop"] = 1 - CFG["AlwOnTop"]

or

CFG["AlwOnTop"] = not CFG["AlwOnTop"]

The complete function could be:

def Mwindow_stayontop(self, event):
    CFG["AlwOnTop"] = 1 - CFG["AlwOnTop"]
    self.SetWindowStyle(wx.DEFAULT_FRAME_STYLE | CFG["AlwOnTop"]*wx.STAY_ON_TOP)

though some might consider that too compacted.

Ned Batchelder
+1  A: 

Here's a short version:

def Mwindow_stayontop(self, event):
    CFG["AlwOnTop"] = not CFG["AlwOnTop"]
    self.setWindowStyle(wx.DEFAULT_FRAME_STYLE | 
                        (wx.STAY_ON_TOP if CFG["AlwOnTop"] else 0))

If you want to consider the AlwOnTop as a boolean property, you can use the fact that 0 is False and 1 is True to your advantage. not will alternate the states.

Santiago Lezica