Найти - Пользователи
Полная версия: Gtk, TextView и строки. Как вывести?
Начало » GUI » Gtk, TextView и строки. Как вывести?
1
delvin-fil
Приветствую всех.
Есть код
 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()

Выхлоп print(out) идет с разбиением на абзацы по предложениям(почему?). Естественно, Gtk.TextView() выводит только последний.
Как исправить? (не смог сформулировать вопрос для гугла)
Заранее спасибо!
delvin-fil
Вопрос решен!
Изменил строку 23. Убрал is_detail_result=True, и все заработало.
 import translators as ts
from langdetect import detect
from textblob import TextBlob
 
import os
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, 'unnamed.png')
print(ICON)
intext = "A comma-separated list of keyboard layouts. A layout may be enclosed in double quotes. Each layout is a name with optional arguments, which is to be parsed by the setxkbmap program. To support changing keyboard layouts, the setxkbmap program must be installed. The first in the list is the default layout. Programs may have their own keyboard layout defined in the winoptions file. The first two letters of a layout are used to locate an icon image file."
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,
                    if_use_cn_host=True)
print(f"{dettext}-{langout}\n")
print (outtext)
 
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"{outtext}")
        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()

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB