Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PySimpleGUI get selected text

Tags:

pysimplegui

New to PySimpleGUI.

I have a multiline input box :

    layout1 = [[sg.Multiline(size=(45,5),key='-IN-')],...
    window1 = sg.Window('Source',layout1,finalize=True)
    event1,values1 = window1.read()

I type in some text and then using the mouse, I highlight a portion of the text. How do I get that selected (highlighted) text?

In Tkinter, I simply used :

        self.title = self.e.selection_get() 

but I like what I've seen of PySimpleGUI and would to try to stick with it.

I have searched here, github and google and haven't found anything about this. Hoping it is something simple and that someone is able to point me in the right direction.

Thanks,

Randy

like image 928
Randy Murray Avatar asked Feb 28 '20 22:02

Randy Murray


1 Answers

In the PySimpleGUI documentation, you'll find a section on "Extending PySimpleGUI". It discusses how to go about using features of tkinter that are not yet implemented in PySimpleGUI.

Each element has a member variable named Widget. This variable contains the underlying widget used in your layout. This variable is your gateway to the tkinter features not yet implemented in PySimpleGUI. It makes extending PySimpleGUI really straighforward.

This is your code with additional code using the Widget variable.

import PySimpleGUI as sg

layout1 = [[sg.Multiline(size=(45, 5), key='-IN-')], [sg.OK(key="-ok-")]]
window1 = sg.Window('Source', layout1, finalize=True)

while True:  # Event Loop
    event, values = window1.read()
    selection = window1['-IN-'].Widget.selection_get()
    print('selection = ', selection)

The important part to pick up from this answer is that all elements have this member variable that can be used to extend PySimpleGUI. This is the most important part of the code:

window1['-IN-'].Widget

It looks up the element based on the key, then gives you the tkinter widget that implements it. At this point, you can make all of the calls that are normally available to you using that widget.

like image 143
Mike from PSG Avatar answered Nov 22 '22 11:11

Mike from PSG