TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: ALL
from: DENNIS LEE BIEBER
date: 2020-02-17 16:28:00
subject: Re: Starting a Python scr

On Mon, 17 Feb 2020 07:52:49 +0100, "R.Wieser" 
declaimed the following:

>
>If you have wx installed, could you please check ?
>(Anyone else who has wx installed is invited too :-) )
>

 While I no doubt do have it installed on at least one machine, I
haven't really used it. My primary usage of Python is quick&dirty programs
typically processing batches of data into some format for later use. No GUI
needed. The other problem is that my wxPython books predate the "Phoenix"
rewrite and may not be applicable.

 Heck, if Windows and/or Linux had an equivalent of Amiga Message Ports
(needed to implement REXX port) I'd likely be using a port of REXX for some
stuff.

 Note that wx.KeyboardState is described as a base class for wx.KeyEvent
-- that means it has to be accessible when KeyEvent determines an event
took place, not the other way around.
https://wxpython.org/Phoenix/docs/html/wx.KeyboardState.html
(And it does look like pre-Phoenix modifiers were only available as
properties of a key event
https://wxpython.org/Phoenix/docs/html/classic_vs_phoenix.html ). Having
said that -- while I can call it anywhere without getting an error, it also
is not detecting changes. And that is even in a keydown/keyup handler. I
have verified that the meta keys WILL generate down/up events. Modifiers do
not generate EVT_CHAR events.


 Okay... I couldn't get wx.KeyboardState() to do anything desirable. It
acted more like a way to SET modifier state!

 BUT... I stumbled onto a wx.GetKeyState(key)

-=-=-=-
pi@rpi3bplus-1:~$ cat wxstate.py
#!/usr/bin/env python3
import wx
import sys

class KeyState(wx.Frame):
    def __init__(self, *args, **kw):
        super(KeyState, self).__init__(*args, **kw)
#        self.ks = wx.KeyboardState()

        print("KeyState init: %s, %s, %s" % (wx.GetKeyState(wx.WXK_ALT),
                wx.GetKeyState(wx.WXK_CONTROL),
                wx.GetKeyState(wx.WXK_SHIFT)))

        if wx.GetKeyState(wx.WXK_CONTROL) and wx.GetKeyState(wx.WXK_SHIFT):
            sys.exit()

        self.wdw = wx.Window(self)

        self.SetFocus()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer)


        self.timer.Start(1000)

    def OnTimer(self, event):
        print("KeyState timer: %s, %s, %s" % (wx.GetKeyState(wx.WXK_ALT),
                wx.GetKeyState(wx.WXK_CONTROL),
                wx.GetKeyState(wx.WXK_SHIFT)))
        if wx.GetKeyState(wx.WXK_CONTROL) and wx.GetKeyState(wx.WXK_ALT):
            sys.exit()

app = wx.App()

frm = KeyState(None, title="Key State Test")

frm.Show()

app.MainLoop()
pi@rpi3bplus-1:~$

-=-=-=-
pi@rpi3bplus-1:~$ ./wxstate.py
KeyState init: False, False, True
KeyState timer: False, False, True
KeyState timer: False, False, False
KeyState timer: False, False, False
KeyState timer: False, False, False
KeyState timer: True, True, False
pi@rpi3bplus-1:~$ ./wxstate.py
KeyState init: False, True, False
KeyState timer: False, True, False
KeyState timer: False, False, False
KeyState timer: False, False, False
KeyState timer: True, True, False
pi@rpi3bplus-1:~$ ./wxstate.py
KeyState init: False, True, True
pi@rpi3bplus-1:~$

-=-=-=-

NOTES:
 First run, held shift key down while pressing  to start the
script.
 Second run, held control key down...
 Third run, held both shift and control down...

 Can not use ALT key in this test as it interferes with  key
operation.

 Due to the test output I can not run it from file manager as it needs a
console. Furthermore -- my configuration for .py files, when double-clicked
results in a dialog asking

[Execute] [Execute in Terminal]  [Open] [Cancel]


 Something that may make you happy! Try this one...
(You may need:  sudo apt install python3-wxgtk4.0 )

-=-=-=-
pi@rpi3bplus-1:~$ cat myApp.py
#!/usr/bin/env python3
import wx
import sys

logout = open("myApp.log", "w")

class GUIConfig(wx.Frame):
    def __init__(self, *args, **kw):
        super(GUIConfig, self).__init__(*args, **kw)

        self.wdw = wx.Window(self)

        self.SetFocus()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.timer.Start(1000)

    def OnTimer(self, event):
        if (wx.GetKeyState(wx.WXK_CONTROL)
                and wx.GetKeyState(wx.WXK_ALT)
                and wx.GetKeyState(wx.WXK_SHIFT)):
            logout.write(" held down -- exit GUI\n")
            sys.exit()

app = wx.App()

if wx.GetKeyState(wx.WXK_CONTROL) and wx.GetKeyState(wx.WXK_ALT):
    logout.write(" held on launch -- Running GUI for config\n")
    frm = GUIConfig(None, title="myApp Config GUI")
    frm.Show()
    app.MainLoop()
else:
    logout.write("Run application in background?\n")
pi@rpi3bplus-1:~$
-=-=-=-

 First time, double-clicked .py file and then clicked [Execute]

pi@rpi3bplus-1:~$ cat myApp.log
Run application in background?

 Second time, double-clicked .py file, then held down  while
clicking [Execute]

pi@rpi3bplus-1:~$ cat myApp.log
 held on launch -- Running GUI for config
 held down -- exit GUI
pi@rpi3bplus-1:~$


 Exiting the GUI and entering the background application is left as an
exercise -- my test code just kills the entire program.



--
 Wulfraed                 Dennis Lee Bieber         AF6VN
 wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.