- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
是否可以使用 Tkinter 为阿拉伯语或希伯来语等 RTL 语言呈现用户界面?我尝试用谷歌搜索“tkinter rtl”,搜索结果令人失望。 Tk wiki表示此时没有比迪支持。
有人在为阿拉伯语或希伯来语语言环境开发 Tkinter 应用程序吗?
最佳答案
我意识到这是一个老问题,但我昨天才开始使用 Tkinter 使用 Python 开发希伯来语应用程序。从右到左 (bidi) 作为框架的一部分不可用,但经过一些谷歌搜索和一些研究后,我设法通过键绑定(bind)和强行重新定位光标来令人信服地伪造它。我的 Entry 小部件保持左对齐,因此希伯来语文本与同一个框上的某些英语的位置大致相同,但这种方法可以很容易地修改为右对齐框。 (或者,右对齐可能会使这更简单)。尽管如此,这就是我所做的。
本质上,您在这里所做的是使用回调、字符代码和索引常量手动强制执行光标位置。此外,您必须考虑箭头键(我的箭头键的行为是朝着它们指向的方向移动。我一直讨厌 RTL 通常如何反转箭头。不过,如果您愿意,这很容易改变。)退格键和德尔键,此外,还必须进行一些手动重新定位。当然,如果您手动跟踪光标,则必须在用户使用鼠标重新定位时更新跟踪变量。下面是我的代码,除了在这里使用全局变量是为了减少解释的复杂性。
# Here, the necessary bindings. We're going to
# have to make modifications on key press, release,
# and on a completed mouse click.
entryWidget.bind("<KeyPress>", rtlPress)
entryWidget.bind("<KeyRelease>", rtlRelease)
entryWidget.bind("<ButtonRelease>", rtlMouse)
接下来是三个回调函数,它们完成我们所有的光标跟踪和重定位。
#With the following functions, keep in mind that we only want the cursor to move RIGHT
#(increase in index) in response to a right arrow press or a DEL. Essentially, we are
#compensating for any movement but these explicit conditions. Since the indexing of the
#cursor position is LTR, holding it in its current position
#while we append more text is
#tantamount to moving it right.
#On key release, if an arrow key has been invoked, we update our tracking variable to
#reflect the new cursor position. If any other key was pressed, we snap the cursor back
#to where it was prior to the keypress to prevent it from moving right and cause the
#next letter to be appended on the left side of the previous letter.
def rtlRelease(event):
global hebCursorPos
if event.keycode==114 or event.keycode==113:
hebCursorPos=event.widget.index(INSERT)
else:
event.widget.icursor(hebCursorPos)
print(str(event.keycode)+" "+str(hebCursorPos))
#On keypress, we must compensate for the natural LTR behavior of backspace(22) and
#del(119)
def rtlPress(event):
global hebCursorPos
#In LTR text entry, a backspace naturally removes the character to the left of
#the cursor.
if event.keycode==22:
length = len(event.widget.get())
#In RTL, the right edge is the beginning of the string, so backspace
#should do nothing.
#If we're at the right edge of the string, we insert a meaningless
#character to be deleted so that it appears to the user as if we have
#done nothing.
if hebCursorPos==length:
event.widget.insert(hebCursorPos, " ")
#In order to cause the backspace to delete the character to the right
#rather than the left of the cursor from the user's perspective, we step
#the cursor forward one. This will cause the backspace to delete the
#character to the left of the new cursor position, which will be the
#character that was to the right of the cursor from the user's
#perspective. If we were at the right end of the line, we insert a space
#and delete it milliseconds later. We do not need to update the cursor's
#position, in the tracking variable, because after the character is
#deleted, it is back at the index from which it started, counting index
#from an LTR perspective.
event.widget.icursor(hebCursorPos+1)
else:
#Del is more of the same. It deletes the character to the right of the
#cursor, but we want it to delete the character to the right.
if event.keycode==119:
#If we're at the left edge of the string, insert a meaningless character
#for the del to delete, so that from the user's perspective it does
#nothing.
if hebCursorPos==0:
event.widget.insert(hebCursorPos, " ")
#Otherwise, we will be stepping the cursor one to the left, so
#that when it deletes the character to its new right, it will be
#deleting the character from what the user thinks is its left.
#Because we are deleting a character from the left of the cursor
#from the user's perspective, there will be fewer characters to
#the left of the cursor once the operation is complete. As
#cursor positioning is tracked as an LTR index, we must update
#our tracking variable.
else:
hebCursorPos-=1
#Now, we snap our cursor to the position of our tracking variable.
#Either we are preventing it from drifting right due to overlapping
#keypresses, or we are repositioning it to maintain the correct index
#after a del.
event.widget.icursor(hebCursorPos)
#Simply put, if the user repositions the cursor with the mouse, track it.
def rtlMouse(event):
global hebCursorPos
hebCursorPos=event.widget.index(INSERT)
希望对您有所帮助!由于它是通过强制光标移动完成的,因此在打字过程中会有轻微的视觉光标抖动,但文本顺序似乎是正确的,并且当用户没有按下中间键时,光标似乎总是指示正确的位置。不过,我并没有声称代码是完美的!
关于 python /Tkinter : Using Tkinter for RTL (right-to-left) languages like Arabic/Hebrew?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4150053/
Ho,我有一个需要导出到 mysql 的 access 数据库,我安装了 MySQL 迁移工具,并且我实际上可以复制数据,问题是表中的希伯来语条目转换为问号(??? ??????)。 我尝试过在 PH
在我的基于 arm 的设备上,希伯来语 QString 显示为垃圾(“?????????”),但“?”的数量正确字符,当使用 qDebug() 发送到控制台时。 在我的 PC (Ubuntu 14.0
我正在尝试读取带有希伯来语列名称的 .txt 文件,但没有成功。 我将示例文件上传到: http://www.talgalili.com/files/aa.txt 我正在尝试命令: read.tabl
我需要同时支持阿拉伯语和希伯来语。我已经添加了 RTL 支持。 我的问题是:在 RTL 中支持阿拉伯语和希伯来语之间有什么区别吗? 最佳答案 是的,有,阿拉伯语中的某些词可能比希伯来语中的词大,反之亦
是否可以使用 Tkinter 为阿拉伯语或希伯来语等 RTL 语言呈现用户界面?我尝试用谷歌搜索“tkinter rtl”,搜索结果令人失望。 Tk wiki表示此时没有比迪支持。 有人在为阿拉伯语或
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
希伯来语字符之一(即元音)的代码是 1463 或 0x05b7 我尝试仅选择包含此字符的单词,但我得到了整个单词列表。 我尝试: DECLARE @d NCHAR set @d = NCHAR(146
我有一个宽字 rune 件(带有希伯来语文本),在记事本中看起来不错(以“UTF-8 编码”保存),在 Notepad++ 中读取良好,当我复制并粘贴到 MS Word 中时,它看起来也不错。但是,当
我是一名优秀的程序员,十分优秀!