简体   繁体   中英

PySimpleGUI: Set and get the cursor position in a multiline widget?

Is there a way to get the position of the cursor in a multiline widget in PySimpleGUI, storing it and putting the cursor back again on a defined position in the text of that widget?

Below you see the code I wrote so far. My aim is that when "jk" in typed in the upper window then the cursor goes down to the input line (that works). There the user can write a command and finish the input pressing (that I have not done yet).

The question is now how to make the cursor jump back in the upper window on the same position where it was before?!

import PySimpleGUI as sg

layout = [  [sg.Multiline(key = 'editor',
                          size = (50, 10), 
                          focus = True, 
                          enable_events = True)],
            [sg.InputText(key ='command', size = (45, 1), ),], ]

window = sg.Window('editor', layout)

while True:
    event, values = window.read()

    if 'jk' in values['editor']:
        # delete jk and jump down in the command line #
        window['editor'].update(values['editor'].replace('jk', ''))
        window.Element('command').SetFocus(force = True)
        
    if event in ('Close Window', None): 
        break
    
window.close()

Any help is appreciated as the is no documentation about setting or getting cursor position in PySimpleGui. Thanks in advance!

Tkinter code required here

Example code show how it work, "jk" to jump from "M1" to "M2", and key to jump back from "M2" to "M1".

import PySimpleGUI as sg

sg.theme("DarkBlue")
sg.set_options(font=('Courier New', 16))

layout = [
    [sg.Multiline('', size=(40, 5), enable_events=True, key='M1')],
    [sg.Multiline('', size=(40, 5), key='M2')],
]
window = sg.Window("Title", layout, finalize=True)

m1, m2 = window["M1"], window['M2']
m2.bind("<Return>", "_Return")

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == "M1" and 'jk' in values["M1"]:
        m1.Widget.delete("insert-2c", "insert")
        m2.set_focus()
    elif event == "M2_Return":
        m1.set_focus()
    print(event, values)

window.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM