0

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 Answers2

0

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()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Thanks a lot Jason for prompt feedback, In fact my sets is more specific, what about case where we need to define sets different than numbers like in our previous exqmple, where I For example : Set A : α, β, π .....etc and Set B : R, T, F ......etc, the idea is just to have one elemnet map to another, instead of Letters and numbers ? Thanks in advance – PyiPath2022 Oct 27 '22 at 07:22
  • Dear @Jason, Could you please give me a hint for that ? Thanks in advance – PyiPath2022 Oct 30 '22 at 07:19
  • Not just an idea, "for example" and "..." about your question. Give us the detail and exact information about what and how it is. People cannot help it if you don't give more. – Jason Yang Oct 30 '22 at 08:35
  • You asked a question here, people answered the question, then you said yes, but this question is not like this ?? – Jason Yang Oct 30 '22 at 08:37
  • Appologize for any misunderstadning, probably my initial query is not well explained, what has been provided is almost what I want, except instead of numbers in set B , I want other strings, example : A=(a, b, c, ...., z), and B=(apple, banana, oragne , .....etc) number of items in A is same as in B, and my desired output is, once I write one item from list A in an InputText , I will get automatically ( in an interactive way), the item apple in another InputText. Thanks in advance – PyiPath2022 Oct 30 '22 at 08:45
  • Just create a variable `table = {'a':'apple', 'b':'banana', 'c':'orange'}`, and mapping each character in `'-IN1-'` to update `'-IN2-'` by the table. Updated as above. – Jason Yang Oct 30 '22 at 09:06
  • Thanks a lot @Jason, you can close this topic :) – PyiPath2022 Oct 30 '22 at 09:35
  • If this answer is accept, you can click tick on the left to accept it, not close it. – Jason Yang Oct 30 '22 at 10:45
  • Done. I'm Newebie in this forum, that whey I miss many things :) – PyiPath2022 Oct 30 '22 at 14:06
0

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

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
  • Thanks Charles for your answer, you got my point , I want to do it interactively and @Jason above has solved that, but he did that between letters and numbers ( as per my initial request), but in fact, I want to have general sets including specials characters, and the reason why I want to do it interactively, because I can't insert text in one set, so I want to get it's equivalaent from another set, did you get this point ? – PyiPath2022 Oct 27 '22 at 08:16
  • @Jason and Charles, Any idea to achieve this ? Thx – PyiPath2022 Oct 27 '22 at 15:59