RichyRich
RichyRich

Reputation: 31

Showing International Characters using Python and Tkinter

Using Python v3 and Tkinter, I'm trying to read a text file that contains International characters and then display them on the screen (e.g. in a Menu) but the characters display wrongly.

a simple example of data in the text file is:- Letzte VTR-Datei öffnen (which I think is German for 'Open recent VTR file' - or something similar)

What I see is the ö character being replaced with something like a capital A with 2 dots on the top. Quite possible all the 'International' characters are replaced with that.

I tried adding encoding = "utf-8" to my source code but it didn't help.

e.g.

from tkinter import *

root = Tk()

encoding = "utf-8"

prompt={}
language_file = open("Control_lang_de.txt", "r")
for pmts in language_file:
    pmt = pmts.strip()
    x = pmt.split("=")
    key = x[0].strip()
    value = x[1].strip()
    prompt[key] = value

language_file.close()
# This reads a line with OpenRecentVTRFile=Letzte VTR-Datei öffnen
# So now have prompt[OpenRecentVTRFile] containing the text.

# Now add to the menu...
main_menu = Menu(root)
root.config(menu=main_menu)
file_menu = Menu(main_menu, bd=2)
main_menu.add_cascade(label=prompt["OpenRecentVTRFile"], menu=file_menu)

root.mainloop()

Upvotes: 0

Views: 107

Answers (1)

RichyRich
RichyRich

Reputation: 31

As the language file was encoded as utf-8, all I had to do was let the python open statement know about that. The solution turned out to be quite simple. I just needed to add encoding="utf-8" to my open statement. When I changed it to language_file = open("Control_lang_de.txt", "r", encoding="utf-8") it worked as wanted.

Thanks to @SylvesterKruin for suggesting that to me.`

Upvotes: 1

Related Questions