How to get character's equivalent from another TextInput using PySimpleGUI?

213 views Asked by At

Dears,

How to get character's equivalent from another TextInput using PySimpleGUI? Let me explain: Suppose I have those sets of data , set A and Set B, my query is once I write one characters in TextInput 1 from Set A I'll get automatically it's equivalent in Set B; For example Set A : A, B, C, D, ........, Z Set B : 1, 2, 3,4, ..........,26

So if I write ABC in TextInut A --> I'll get : 123 in TextInput B

Thanks in advance

import PySimpleGUI as sg

enter image description here

2

There are 2 answers

2
Charles Merriam On BEST ANSWER

My apologies if I misunderstand your question.

First, special characters, like ☯, ∫, β, etc., are just Unicode characters. You can type them directly into your editor or use the Unicode escape codes. You might see this question for more help.

Second, it is unclear when you want to make this mapping. It is easiest if you type characters and then map at the end. If you want to do interactively that is harder. You can get each individual keyboard event; see (this answer)[https://stackoverflow.com/a/74214510/1320510] for an example. Because I know of no way of the exact position, you might be better getting the events, and writing the display to a second label. I would need to more a bit more about what you are doing.

Keep hacking! Keep notes.

Charles

9
Jason Yang On

Set option enable_events=True to A, map each char in values[A] by dictionary {'A':'1', ...}, then update B with the result when event A.

Demo Code

import string
import PySimpleGUI as sg

table = {char:str(i+1) for i, char in enumerate(string.ascii_uppercase)}

layout = [
    [sg.Input(enable_events=True, key='-IN1-')],
    [sg.Input(key='-IN2-')],
]
window = sg.Window('Main Window', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == '-IN1-':
        text1 = values['-IN1-']
        text2 = ''.join([table[char] if char in string.ascii_uppercase else char for char in text1])
        window['-IN2-'].update(text2)

window.close()

For different case, like table = {'a':'apple', 'b':'banana', 'c':'orange'}

import string
import PySimpleGUI as sg

table = {'a':'apple', 'b':'banana', 'c':'orange'}

layout = [
    [sg.Input(enable_events=True, key='-IN1-')],
    [sg.Input(key='-IN2-')],
]
window = sg.Window('Main Window', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == '-IN1-':
        text1 = values['-IN1-']
        text2 = ''.join([table[char] if char in table else char for char in text1])
        window['-IN2-'].update(text2)

window.close()