我想在我的 python 应用程序中使用阿拉伯语,但它不起作用。我尝试了以下方法:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
.
.
.
welcMsg = 'مرحبا'
welcome_label = ttk.Label(header, text=welcMsg, font=(
"KacstBook", 24)).grid(row=0, column=0, pady=20)
我也尝试添加 welcMsg = welcMsg.encode("windows-1256", "ignore")
但结果总是这样它也发生在 tkinter Entry 和 Text
searchField = ttk.Entry(tab3, width=50)
textBox = Text(tab4, width=45, height=15, font=("KacstOffice", 16), selectbackground="yellow",
selectforeground="black", undo=True, yscrollcommand=text_scroll.set, wrap=WORD)
那么还有什么我可以尝试使用标签、条目和文本的吗?注意:我使用的是 Ubuntu 18.04 仿生
最佳答案
Tkinter 没有双向支持(根据 tk 的源代码),这意味着像阿拉伯语这样的 RTL(从右到左)语言将显示不正确,有 2 个问题:
1- 字母将以相反的顺序显示。
2- 字母连接不正确。
阿拉伯语在 Windows 上正确显示的原因是因为双向支持是由操作系统处理的,而在 Linux 中并非如此
要在 linux 上解决此问题,您可以使用 AwesomeTkinter包,它可以为标签和条目添加双向支持(也在编辑时)
import tkinter as tk
import awesometkinter as atk
root = tk.Tk()
welcMsg = 'مرحبا'
# text display incorrectly on linux without bidi support
tk.Label(root, text=welcMsg).pack()
entry = tk.Entry(root, justify='right')
entry.pack()
lbl = tk.Label(root)
lbl.pack()
# adding bidi support for widgets
atk.add_bidi_support(lbl)
atk.add_bidi_support(entry)
# Now we have a new set() and get() methods to set and get text on a widget
# these methods added by atk.add_bidi_support() and doesn't exist in standard widgets.
entry.set(welcMsg)
lbl.set(welcMsg)
root.mainloop()
输出:注意:您可以通过
pip install awesometkinter
安装 awesometkinter
关于python - 在 tkinter 中使用阿拉伯语文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66283291/