Bruno 'Shady'
Bruno 'Shady'

Reputation: 4516

How can I set a true/false variable with if?

It's kinda hard to explain, but I will try my best.

I have this part on my code

def hideConsole():
    hideConsole = win32console.GetConsoleWindow()
    win32gui.ShowWindow(hideConsole, 0)

which hides the console, and I have this part to enable it

def onKeyboardEvent(event):
    if event.KeyID == 192 and event.Alt == 32:
        hideConsole()
    return True

how can I make a "system" where when I press the key combination one time, the console hides, and the next time, the console will show up? (change the hideConsole, 1 value)

Upvotes: 1

Views: 2103

Answers (5)

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

With a boolean variable, something like this:

class Console(object):
    def __init__(self):
        self.is_hidden = False
        self.handle    = win32console.GetConsoleWindow()

    def toggle(self):
        win32gui.ShowWindow(self.handle, 1 if self.is_hidden else 0)
        self.is_hidden = not self.is_hidden

Upvotes: 1

cdhowie
cdhowie

Reputation: 169018

You need to maintain state somehow:

hidden = False

def toggleConsoleVisibility():
    global hidden

    hideConsole = win32console.GetConsoleWindow()
    win32gui.ShowWindow(hideConsole, 1 if hidden else 0)
    hidden = not hidden

def onKeyboardEvent(event):
    if event.KeyID == 192 and event.Alt == 32:
        toggleConsoleVisibility()
    return True

If possible, write this as part of a class. Then you can keep the hidden variable encapsulated by the class instead of floating around in your global namespace.

Upvotes: 0

Gringo Suave
Gringo Suave

Reputation: 31880

con_visible = True

def setVisibility(visible):
    global con_visible
    hideConsole = win32console.GetConsoleWindow()
    win32gui.ShowWindow(hideConsole, int(visible))
    con_visible = bool(visible)

def onKeyboardEvent(event):
    if event.KeyID == 192 and event.Alt == 32:
        if con_visible:
            setVisibility(False)
        else:
            setVisibility(True)
    return True

If the console holds its visibility state internally you could preferably use that instead of a global variable.

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208475

You can use a function attribute that you switch between true and false on each call:

def toggleConsole():
    toggleConsole.show = not getattr(toggleConsole, "show", True)
    console = win32console.GetConsoleWindow()
    win32gui.ShowWindow(console, int(toggleConsole.show))

Here is a quick example of how this works:

>>> def test():
...     test.show = not getattr(test, "show", True)
...     print int(test.show)
... 
>>> test()
0
>>> test()
1
>>> test()
0

Upvotes: 1

Omri Barel
Omri Barel

Reputation: 9480

You can use a state variable hideValue with initial value 0, and for each keyboard event do:

hideValue = 1 - hideValue

This will toggle hideValue between 0 and 1.

Then you can call win32gui.ShowWindow(hideConsole, hideValue).

Upvotes: 0

Related Questions