Python/Tkinter:将Tkinter用于RTL(从右到左)语言,如阿拉伯语/希伯来语?
是否可以使用Tkinter为RTL语言(如阿拉伯语或希伯来语)呈现用户界面?我试着用谷歌搜索“tkinter rtl”,结果令人失望。表示目前没有bidi支持Python/Tkinter:将Tkinter用于RTL(从右到左)语言,如阿拉伯语/希伯来语?,python,localization,tkinter,right-to-left,Python,Localization,Tkinter,Right To Left,是否可以使用Tkinter为RTL语言(如阿拉伯语或希伯来语)呈现用户界面?我试着用谷歌搜索“tkinter rtl”,结果令人失望。表示目前没有bidi支持 有人在为阿拉伯语或希伯来语地区开发Tkinter应用程序吗?我意识到这是一个老问题,但我昨天刚开始与Tkinter合作,用Python开发希伯来语应用程序。从右向左(bidi)并不是框架的一部分,但经过一点谷歌搜索和一些研究,我通过键绑定和强制重新定位光标,成功地伪造了它。“我的输入”小部件保持左对齐,这样希伯来文文本与同一框中的一些英文
有人在为阿拉伯语或希伯来语地区开发Tkinter应用程序吗?我意识到这是一个老问题,但我昨天刚开始与Tkinter合作,用Python开发希伯来语应用程序。从右向左(bidi)并不是框架的一部分,但经过一点谷歌搜索和一些研究,我通过键绑定和强制重新定位光标,成功地伪造了它。“我的输入”小部件保持左对齐,这样希伯来文文本与同一框中的一些英文文本处于大致相同的位置,但这种方法可以很容易地修改为右对齐框。(或者,正确的理由可能会让这更简单)。尽管如此,我还是这么做了 实际上,您在这里所做的是使用回调、字符代码和索引常量手动强制执行光标位置。此外,您必须考虑箭头键(我的箭头键的行为是沿着箭头指向的方向移动。我一直讨厌RTL通常反转箭头的方式。不过,如果您不喜欢,这很容易更改。)退格和删除也必须引起一些手动重新定位。当然,如果要手动跟踪光标,则必须在用户使用鼠标重新定位光标时更新跟踪变量。下面是我的代码,唯一的例外是,在这里使用global是为了从解释中去除一点复杂性
# 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)
希望这有帮助!由于它是通过强制光标移动来完成的,因此在键入过程中会出现轻微的视觉光标抖动,但文本顺序似乎是正确的,并且当用户不在按键中间时,光标似乎总是指示正确的位置。不过,我并没有声称代码是完美的 这可能也不能解决整个问题,但它可以解决显示问题,我认为这是主要问题 基本上,你需要两件事来颠倒角色顺序,让它们结合在一起
我使用了这个,它可以很好地处理没有变音符号的简单单词,但在某些情况下仍然有问题。尝试将词条或其他元素设置为“jameel noori nastaleeq”或任何其他乌尔都语字体关于这个的更多信息,请访问:2011年左右,tk和tkinter以及IDLE使用Windows支持在Windows上支持bidi希伯来语和阿拉伯语。我只是在IDLE上的字体选择示例中添加了其中的一些,这样人们就可以看到在他们的特定系统上哪些是有效的,哪些是无效的。这是一个可怕的黑客攻击,只解决了阿拉伯语/希伯来语文本呈现中的一小部分问题。
#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)