Python/Tkinter:如何在阿拉伯语/希伯来语等RTL语言中使用Tkinter?
可以用Tkinter来制作阿拉伯语或希伯来语这样的从右到左的语言的用户界面吗?我试着在网上搜索“tkinter rtl”,结果让我有点失望。Tk wiki上说现在没有双向文本支持。
有没有人在为阿拉伯语或希伯来语的环境开发Tkinter应用程序呢?
3 个回答
试着把输入框或其他元素的字体设置为“jameel noori nastaleeq”或者其他的乌尔都字体。
这可能并不能完全解决问题,但我觉得它能解决我看到的主要问题,就是显示方面的问题。
基本上,你需要两样东西来反转字符的顺序,并让它们连接在一起。我使用了这个重塑工具,它在处理没有音标的简单单词时效果很好,比如الحركات
,但在某些情况下还是有点问题。
我知道这个问题有点老,但我昨天刚开始用Tkinter来开发一个希伯来语的Python应用。这个框架本身不支持从右到左的文本(也就是双向文本),不过经过一些谷歌搜索和研究,我找到了一种方法,通过键绑定和强制移动光标来模拟这个效果。我的输入框默认是左对齐的,这样希伯来语的文本大致和同一个框里的英文文本在同一位置,但这个方法可以很容易地调整成右对齐的样式。(或者,右对齐可能会让事情变得更简单。)不管怎样,下面是我做的事情。
基本上,你在这里做的就是手动控制光标的位置,使用回调函数、字符代码和索引常量。同时,你还得考虑到方向键(我的方向键是按照它们指向的方向移动的。我一直不喜欢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)
希望这对你有帮助!由于是通过强制移动光标来实现的,所以在输入时光标会有一点小抖动,但文本的顺序看起来是正确的,而且当用户没有按键时,光标似乎总是指向正确的位置。不过,我并不保证代码是完美的!