python - 在 tkinter 中使用阿拉伯语文本

标签 python ubuntu tkinter utf-8 arabic

我想在我的 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")
但结果总是这样
enter image description here
它也发生在 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()
输出:
enter image description here
注意:您可以通过 pip install awesometkinter 安装 awesometkinter

关于python - 在 tkinter 中使用阿拉伯语文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66283291/

相关文章:

python - 如何使用 LibVLC 更改 MediaListPlayer 中的播放音量?

python - [Python]如何对包含两个变量的字符串进行排序?

python - 如何使用 django 信号访问字段

python - Python 中 Tkinter Canvas 上 .jpg 图像的滚动条

Python 3.x tkinter 导入错误

python - 从组合(文字 ('@' )+ 'spec' )更改为关键字 ('@spec' )删除空格

python - 如何使用 Python 从结果文件中获取设备的特定详细信息

docker - 如何解决 docker-compose 中的 "driver failed programming external connectivity"?

linux - 为什么 "$ git log --graph "不工作

python - 在运行时单击时如何更改按钮的颜色?