8

Tkinter を使用して、アラビア語やヘブライ語などの RTL 言語のユーザー インターフェイスをレンダリングすることはできますか? 「tkinter rtl」でグーグル検索してみましたが、検索結果は期待外れでした。Tk wikiは、現時点で双方向サポートがないことを示しています。

アラビア語またはヘブライ語のロケール用に Tkinter アプリケーションを開発している人はいますか?

4

3 に答える 3

4

これは古い質問だと思いますが、昨日 Tkinter を使用して Python でヘブライ語アプリケーションを開発し始めたばかりです。右から左 (bidi) はフレームワークの一部として利用できませんが、Google で少し検索して調査した結果、キーバインドとカーソルの強制的な再配置によって説得力のある偽造に成功しました。My 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)

次に、カーソルの追跡と再配置のすべてを行う 3 つのコールバック関数です。

#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)

お役に立てれば!これは強制的なカーソルの動きによって行われるため、入力中にカーソルがわずかに視覚的に揺れますが、テキストの順序は正しいように見え、ユーザーがキーを押していないときは常にカーソルが正しい位置を示しているように見えます。ただし、コードの完成度を主張しているわけではありません。

于 2011-10-23T05:27:36.263 に答える
0

これでも問題全体が解決するわけではありませんが、主な問題である表示の問題は解決する可能性があります。

基本的に、文字の順序を逆にして結合させるには2つのものが必要です。このリシェイパーを使用しました。分音記号のない単純な単語ではうまく機能しましالحركاتたが、場合によってはまだバグがあります。

于 2014-07-29T14:22:33.363 に答える