0

Is there any way to change the default input language in tkinter?

Here's the code:

from tkinter import *

root = Tk()

text = Text(root , width = 65 , height = 20 , font = "consolas 14")
text.pack()

mainloop()

Here, when I type some text into the text widget, it is being typed in english.

What I want is to type the text in some other language.

Is there any way to achieve this in tkinter?

It would be great if anyone could help me out.

Lenovo 360
  • 569
  • 1
  • 7
  • 27
  • I am not sure, but have u tried with some german font `font =` ? – Epsi95 Feb 15 '21 at 04:40
  • I am not aware of any font that can display german characters. – Lenovo 360 Feb 15 '21 at 04:58
  • Even if there is a font that can display German characters, how can I type in some other language? I am asking this because I mentioned 'German' in my question just as an example. – Lenovo 360 Feb 15 '21 at 05:15
  • 1
    Input language is an OS level setting. – acw1668 Feb 15 '21 at 05:19
  • Using a french keyboard I was able to type éèáàâô using keys or key combinations into your GUI. Is your question how to generate characters not available from the keyboard? – Tls Chris Feb 15 '21 at 11:50
  • Yes, that is my question. I don't want to use keyboard shortcuts or change the input language of my whole device. I only want to change the input language of my text widget. Is this possible? – Lenovo 360 Feb 15 '21 at 16:07

1 Answers1

0

On my MacOS PC, UK Keyboard, I can use various key combinations to get German and French characters. Alt-a: å
Alt e followed by e: é
Alt u followed by o: ö or by u: ü by a: ä by A: Ä
Alt i followed by a: â
Step 1 would be to experiment with that.

2nd Option If only a few characters are required map them to e.g. the F Keys

import tkinter as tk

root = tk.Tk()

text = tk.Text( root, width = 50, height = 20, font = ( 'Arial', 20 ) )
text.grid()

key_map =  { 'F5': 'ü', 'F6': 'ö', 'F13': 'ß', 'F14': 'á', 'F15': 'é' }
# Map the function keys to the characters required.
# f5, f6, f13, etc return the mapped characters.
# The other F keys are used for system activities from my keyboard.

def do_key( event ):
    char = key_map.get( event.keysym )
    if char:
        text.insert( tk.INSERT, char )
        return 'break'  # Stops the event being passed to the text box.

text.bind( '<KeyPress>', do_key )

root.mainloop()

3rd Option A more thorough approach may be to open a second 'keyboard' window that can send characters to the text box.

import tkinter as tk

root = tk.Tk()

text = tk.Text( root, width = 50, height = 20, font = ( 'Arial', 20 ) )
text.grid()

key_map =  { 'F5': 'ü', 'F6': 'ö', 'F13': 'ß', 'F14': 'á', 'F15': 'é' }

def make_keys( char ):
    def do_key():
        text.insert( tk.INSERT, char )
        return 'break'
    return do_key

def get_key( event ):
    master = tk.Toplevel( root )
    for col, v in enumerate( key_map.values()):
        tk.Button( master, text = v, command = make_keys( v )).grid( row = 0, column = col )
    return 'break'

text.bind( '<KeyPress-F5>', get_key )
# Press F5 in the text box to open the keyboard.

root.mainloop()

With this option the extra window could be permanently open or a further frame in the GUI instead of a separate window.

There may be OS specific ways of doing this more neatly or options in tkinter that I don't know about but this should give some ideas to explore.

Tls Chris
  • 3,564
  • 1
  • 9
  • 24
  • Hi, thanks for your answer. Though this helped, this is not exactly what I am looking for. If I use keyboard shortcuts, I won't be able to type the text in any language I want. What I want is that the user should be able to select any language he/she wants and change the keyboard inputs according to that language. Is there any way to achieve this? – Lenovo 360 Feb 16 '21 at 04:06
  • I think you'll need to research how to use a different, assumed, keyboard layout within the operating system. You seem to want to e.g. Type in a word processor with a UK keyboard but switch to a Greek keyboard to type in your GUI, using physically the same keyboard. I've got no idea how todo that. – Tls Chris Feb 16 '21 at 12:45