我正在使用 tkinter 構建一個翻譯應用程式,并且我試圖讓翻譯的結果出現在新根視窗的新行上。所以如果用戶翻譯
一二
_
_
新的 tkinter 根視窗應該顯示
聯合國
辦事處
tres
這是顯示翻譯單詞的功能
def display_translated(text_to_trans):
root2 = Tk() # new root window
root2.title('Translated text')
root2.geometry('200x200')
label2 = Label(root2, text='This is the translated text') # label to explain what the root window is showing
label2.pack()
display_words = [] # list of empty words that will be appended with the list of translated words that was passed to the function
for item in text_to_trans:
display_words.append(item.text)
textbox1 = Text(root2, height=10, width=20, font=("Arial", 20), bg='yellow')
textbox1.pack()
textbox1.insert(1.0, display_words)
我的猜測是問題在于我在螢屏上顯示單詞的方式(display_words串列),但我不確定是否有更好的方法來獲取翻譯后的單詞。
編輯:當我得到相反的是
uno dos tres.
uj5u.com熱心網友回復:
您需要在每個元素之間顯式插入換行符。最簡單的方法是join:
textbox1.insert("1.0", "\n".join(display_words))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519555.html
上一篇:在Python中查找和替換字符
下一篇:單擊按鈕后如何使表單出現?
