如何使弹出窗口显示普通文本翻译,而不显示<kivy.uix.textinput.TextInput®0x0000000003FCC438>?

时间:2020-10-22 15:49:05

标签: python python-3.x kivy

我的kivy弹出窗口输出如下内容:

<kivy.uix.textinput.TextInput об'єкт в 0x0000000003FC6358>

但是我希望它是翻译。

这是我的代码:

from googletrans import Translator
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup
lang = "uk"

class my(GridLayout, Widget):
    def __init__ (self, **kwargs):
       super(my, self).__init__( **kwargs)
       self.cols = 1
       self.inside= GridLayout()
       self.inside.cols=2

       self.inside.add_widget(Label(text= "Text:"))
       self.Text_that_we_need_to_translate = TextInput(multiline=True)
       self.inside.add_widget(self.Text_that_we_need_to_translate)

       self.add_widget(self.inside)
       self.submit= Button(text="submit", font_size=40)
       self.submit.bind(on_press=self.press)
       self.inside.add_widget(self.submit)
       self.clear = Button(text= "clear")
       self.clear.bind(on_press= self.clear_all)
       self.inside.add_widget(self.clear)

    def popup(self, res):
        self.popup_ = Popup(title='Translation:', content=Label(
            text = str(res.text)), size=(200,200), auto_dismiss=False)
        print(res.text)
        self.popup_.open(res)

    def clear_all(self,some):
        self.Text_that_we_need_to_translate.text= ""
        self.user_text = ""

    def press(self, of):
        global res
        text_ = str(self.Text_that_we_need_to_translate)
        tr = Translator()
        res = tr.translate(text_, dest=lang)
        self.popup(res)

class MyApp(App):
    def build(self):
        return my()

if __name__== "__main__":
    MyApp().run()

1 个答案:

答案 0 :(得分:0)

我相信唯一的问题是您正在将TextInput小部件的字符串版本传递给翻译器。该行:

text_ = str(self.Text_that_we_need_to_translate)

只需要更改为:

text_ = str(self.Text_that_we_need_to_translate.text)

,以便您实际上是在传递文本,而不是TextInput小部件本身。