A
A
Alex F2020-08-01 03:44:35
Python
Alex F, 2020-08-01 03:44:35

Gtk, TextView and Strings. How to withdraw?

Welcome all.

There is a code
import os
import translators as ts
from langdetect import detect
from textblob import TextBlob
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango

CURRDIR = os.path.dirname(os.path.abspath(__file__))
ICON = os.path.join(CURRDIR, 'icon.xpm')

intext = "A Gtk.TextView displays the text stored in a Gtk.TextBuffer. However, most text manipulation is accomplished with iterators, represented by a Gtk.TextIter - a position between two characters in the text buffer. Iterators are not valid indefinitely; whenever the buffer is modified in a way that affects the contents of the buffer, all outstanding iterators become invalid. Because of this, iterators can’t be used to preserve positions across buffer modifications. To preserve a position, we use a Gtk.TextMark, that can be set visible with visible(True). A text buffer contains two built-in marks; an \"insert\" mark (the position of the cursor) and the \"selection_bound\" mark."
dettext = detect(intext)
if dettext == 'ru':
    langout = 'en'
else:
    langout = 'ru'
blob = TextBlob(intext)
number = len((blob.sentences))
print(number)
outtext = ts.google(intext,
                    to_language=langout,
                    is_detail_result=True,
                    if_use_cn_host=True)
print(f"{dettext}-{langout}\n")
i = 0
while i < number:
    out = outtext[0][i][0]
    print(out)
    i += 1

class TextViewWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title=f"Translate {dettext}-{langout}")
        self.set_default_size(1000, 350)
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
        self.grid = Gtk.Grid()
        self.add(self.grid)
        self.create_textview()
        self.create_toolbar()
        self.key_Esc = Gdk.keyval_from_name("Escape")
        self.connect("key-press-event", self._key)

    def create_toolbar(self):
        toolbar = Gtk.Toolbar()
        self.grid.attach(toolbar, 1, 1, 1, 1)
        new_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CLOSE)
        new_button.set_is_important(True)
        toolbar.insert(new_button, 0)
        new_button.connect("clicked", self.on_button_clicked, self.tag_bold)
        new_button.show()

    def on_button_clicked(self, widget, tag):
        Gtk.main_quit()

    def create_textview(self):
        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.set_hexpand(True)
        scrolledwindow.set_vexpand(True)
        self.grid.attach(scrolledwindow, 0, 0, 2, 1)
        self.textview = Gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        self.textbuffer.set_text(f"{out}")
        scrolledwindow.add(self.textview)
        self.textview.set_wrap_mode(Gtk.WrapMode.WORD)
        self.tag_bold = self.textbuffer.create_tag("bold",
                                                   weight=Pango.Weight.BOLD)
        self.textview.modify_font(Pango.FontDescription('Menlo Regular 24'))

    def _key(self, widg, event):
        if event.keyval == self.key_Esc:
            Gtk.main_quit()

win = TextViewWindow()
win.connect("destroy", Gtk.main_quit)
win.set_icon_from_file(ICON)
win.show_all()
Gtk.main()

5f24bc607c56b652123093.png
The print(out) output comes with paragraph breaks. Naturally, Gtk.TextView() only outputs the latter.
How to fix? (I couldn't formulate a question for Google)
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex F, 2020-08-04
@delvin-fil

Decided!

outtext = ts.google(intext,
                    to_language=langout,
                    is_detail_result=True,
                    if_use_cn_host=True)

removed is_detail_result=True
outtext = ts.google(intext,
                    to_language=langout,
                    if_use_cn_host=True)

and everything worked as intended.
5f28b7c319e29631470959.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question