2010-11-10 106 views
6

是否可以使用Tkinter爲RTL語言(如阿拉伯語或希伯來語)呈現用戶界面?我嘗試在「tkinter rtl」上搜索,搜索結果令人失望。 Tk wiki表示目前沒有雙向支持。Python/Tkinter:使用Tkinter for RTL(從右到左)的語言,如阿拉伯語/希伯來語?

有人正在開發阿拉伯語或希伯來語語言環境的Tkinter應用程序嗎?

+0

一些在這個:http://wiki.tcl.tk/699 – 2011-07-22 02:16:06

+0

自從2011左右,使用從Windows支持傳統知識,因此Tkinter的和空閒的支持比迪希伯來語和阿拉伯語Windows上。 https://wiki.tcl.tk/3158。我只是在IDLE上的字體選擇示例中添加了一些,這樣人們就可以看到它們的特定系統上的工作與否。 – 2017-11-13 00:05:22

回答

3

我意識到這是一個老問題,但我剛剛開始與Tkinter一起工作,以開發Python中的希伯來語應用程序。從右到左(比迪)不是框架的一部分,但經過一些Google搜索和一些研究後,我設法通過鍵綁定和強制重新定位光標來令人信服地僞造它。我的輸入小部件保留了理由,以便希伯來文本與同一個盒子上的某些英文大致處於相同的位置,但這種方法可以很容易地修改爲右對齊框。 (或者,正確的理由可能會使這個更簡單)。儘管如此,這是我所做的。

基本上你在這裏做的是使用回調,字符代碼和索引常量手動強制執行遊標位置。另外,你必須考慮到箭頭鍵(我的行爲方向與它所指向的方向一致),我一直都很討厭RTL通常會如何反轉箭頭,但如果不這樣,這很容易改變。)Backspace和Del,還必須引起一些手動重新定位。當然,如果您要手動跟蹤光標,則必須在用戶使用鼠標重新定位時更新跟蹤變量。下面是我的代碼,不同之處在於,在這裏使用全局是爲了從解釋中刪除一些複雜的東西。

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

希望這會有所幫助!由於它是通過強制光標移動來實現的,因此在打字過程中會出現輕微的可視光標抖動,但是文本排序似乎是正確的,並且光標在用戶不是中鍵時似乎總是指示正確的位置。不過,我並沒有提出任何代碼完美的主張。

+0

這是一個可怕的黑客攻擊,只能解決阿拉伯/希伯來語文本渲染帶來的一小部分問題。 – dietr 2012-04-05 15:02:14

0

這也許不能解決整個問題,但它可以解決顯示問題,我看到的主要問題。

基本上你將需要兩件事情顛倒字符順序,並讓他們聯合起來 我用這個reshaper,它工作得很好用簡單的話沒有變音符號الحركات,但它仍然在某些情況下馬車。

相關問題