non-blocking getkey?

eryksun eryksun at gmail.com
Fri Nov 20 03:43:13 EST 2015


On Thu, Nov 19, 2015 at 10:31 AM, Michael Torrie <torriem at gmail.com> wrote:
> One windows it might be possible to use the win32 api to enumerate the
> windows, find your console window and switch to it.

You can call GetConsoleWindow [1] and then SetForegroundWindow [2].

    import os
    import sys

    try:
        import tkinter
        from tkinter import filedialog
    except ImportError:
        import Tkinter as tkinter
        import tkFileDialog as filedialog
        input = raw_input

    if sys.platform == 'win32':
        import ctypes

        kernel32 = ctypes.WinDLL('kernel32')
        user32 = ctypes.WinDLL('user32')

        def setfgwin():
            hcon = kernel32.GetConsoleWindow()
            if hcon and user32.SetForegroundWindow(hcon):
                return True
            return False

    def get_filename():
        filename = filedialog.askopenfilename()
        setfgwin()
        return os.path.normpath(filename)

    if __name__ == '__main__':
        root = tkinter.Tk()
        root.withdraw()
        filename = get_filename()
        print('filename: %s' % filename)
        input('press enter...')

[1]: https://msdn.microsoft.com/en-us/library/ms683175
[2]: https://msdn.microsoft.com/en-us/library/ms633539



More information about the Python-list mailing list