Skip to content

Instantly share code, notes, and snippets.

@kat0h
Last active February 17, 2021 16:16
Show Gist options
  • Save kat0h/5f31d7a814c1f9aa6c64ac06bce0706b to your computer and use it in GitHub Desktop.
Save kat0h/5f31d7a814c1f9aa6c64ac06bce0706b to your computer and use it in GitHub Desktop.
# This code is licensed under CC0
import PySimpleGUI as sg
import pyautogui
def get_cursorpos():
return pyautogui.position()
def get_winpos(window):
return window.CurrentLocation()
sg.theme('Default1')
layout = [
[sg.T(size=(20, 1), justification='center', key='-mousepos-')],
[sg.T(size=(20, 1), justification='center', key='-winpos-')],
]
window = sg.Window('Pos', layout)
while True:
event, values = window.read(timeout=1000/60, timeout_key='-timeout-')
if event in (None,):
break
elif event in '-timeout-':
cursorpos = get_cursorpos()
window['-mousepos-'].update("CursorPos x:{} y:{}".format(cursorpos[0], cursorpos[1]))
winpos = get_winpos(window)
window['-winpos-'].update("WindowPos x:{} y:{}".format(winpos[0], winpos[1]))
@PySimpleGUI
Copy link

PySimpleGUI commented Feb 16, 2021

I have a few suggestions. Some are simply personal preference, others, such as event in '-timeout-' are an actual bug fix.

Here's what I changed:

  • Renamed "window" parameter and added a type onto it. PyCharm warned that window shadowed your other variable
  • Called current_location instead of CurrentLocation. Always use the lower case version of methods as they are PEP8 compliant
  • Tested for sg.WIN_CLOSED instead of None. It's clearer and allows for changes in PySimpleGUI to not impact you if None is changed to something else in the future to indicate window closed
  • Removed the test for timeout.
    • There was a problem in using is for the if. Read about the is keyword to understand why.
    • You likely want to update the stuff regardless of what event was received (button click for example... you still want to update the info)
    • You can always add back the if when it's important to distinguish
  • Upper case key names - personal pref to follow the PSG coding guidelines
  • Justification = 'c' is all that's needed. Since you're using sg.T figured compact is what you're after
  • Combined your "get" functions into 1 function

Alternate layout and window just for fun that does the same thing. Saved typing the stuff by only having on the Window creation. Not needed and has downsides if the layout changes in the future:

layout = [[sg.T(key='-MOUSEPOS-')],
          [sg.T(key='-WINPOS-')],]

window = sg.Window('Window & Cursor Position', layout, element_justification='c', default_element_size=(20,1), auto_size_text=False)

My code:

# This code is licensed under CC0
import PySimpleGUI as sg
import pyautogui

def get_pos(win:sg.Window):
    return pyautogui.position(), win.current_location()

sg.theme('Default1')

layout = [[sg.T(size=(20, 1), justification='c', key='-MOUSEPOS-')],
          [sg.T(size=(20, 1), justification='c', key='-WINPOS-')],]

window = sg.Window('Window & Cursor Position', layout)

while True:
    event, values = window.read(timeout=1000//60)

    if event == sg.WIN_CLOSED:
        break
    cursorpos, winpos = get_pos(window)
    window['-MOUSEPOS-'].update("CursorPos x:{} y:{}".format(cursorpos[0], cursorpos[1]))
    window['-WINPOS-'].update("WindowPos x:{} y:{}".format(winpos[0], winpos[1]))

window.close()

@PySimpleGUI
Copy link

BTW, it's EASY for someone to come along after you've done all the hard work, and make changes like mine. I did nothing difficult. YOU did all the hard work. I tweaked things.

@kat0h
Copy link
Author

kat0h commented Feb 17, 2021

Thank you. I didn't know the good way to write python and PySimpleGUI.
So your point was very helphul.
Thank you very much!

I made this (https://github.com/kato-k/parroteye) program to learn PySimpleGUI.
I don't make use of what I have learned yet, so I will refrect changes.

(I'm not good at English, so my text may be hard to read)

@PySimpleGUI
Copy link

You're welcome. Your English is great. Your text is not hard to read. I honestly didn't know English wasn't your first language. That's an added level of difficulty.

I'm impressed you have learned something not published anywhere that I know of, using pyautogui and PySimpleGUI together. You also ran an async event loop, an advanced feature.

Hoping my suggestions (there were a LOT of them) were not taken as criticism. Your code WORKED. Nothing I suggested as a serious problem. And as I said in my previous message, it's really easy for someone to suggest improvements to code that's already written. I'm glad you found it useful and are using it as a learning opportunity! Really what I was hoping for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment