Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

python - 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!

question from:https://stackoverflow.com/questions/65923933/pysimplegui-set-and-get-the-cursor-position-in-a-multiline-widget

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...