codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Gtk.TextBuffer problem on converting from GTK+ 2 to GTK+ 3


On 2020-07-30 17:23, Chris Green wrote:
> I'm converting a program from Python 2 gtk+ 2 to Python 3 and gtk+ 3.
> It's mostly gone reasonably smoothly but I can't get multi-line text
> entry to work.
> 
> The class that provides text entry (both single and multi-line) is:-
> 
>      #
>      #
>      # Field on the GUI
>      #
>      class guiField:
>          widget = None
>          lines = 1
>          #
>          #
>          # Initialise the field
>          #
>          def __init__(self, lines=1):
>              self.lines = lines
>              if (self.lines > 1):
>                  self.buffer = Gtk.TextBuffer()
>                  self.view = Gtk.TextView()
>                  self.view.set_size_request(-1, 24 * lines)
>                  self.view.set_accepts_tab(False)
>                  # self.widget = self.view
>                  self.widget = Gtk.ScrolledWindow()
>                  self.widget.add(self.view)
>                  self.widget.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
>                  self.widget.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
>              else:
>                  self.widget = Gtk.Entry()
>          #
>          #
>          # get text from field
>          #
>          def get_text(self):
>              if (self.lines > 1):
>                  [start, end] = self.buffer.get_bounds()
>                  print(self.buffer.get_text(start, end, False))
>                  return self.buffer.get_text(start, end, False)
>              else:
>                  return self.widget.get_text()
>          #
>          #
>          # put text into a field
>          #
>          def set_text(self, text):
>              if (self.lines > 1):
>                  self.buffer.set_text(text)
>              else:
>                  self.widget.set_text(text)
> 
> 
> The single line Gtk.Entry widgets work fine, text gets loaded, I can
> change it and save it.
> 
> The multi-line Gtk.TextBuffer ones refuse to work at all, no errors or
> anything but text won't load into them or save out of them.  They used
> to work fine in gtk+ 2.
> 
> Text loaded into the Gtk.TextBuffer using set_text() doesn't appear,
> the text boxes are empty even though the 'text' parameter does have a
> string in it (I've checked with a print()).  I can enter text using
> the keyboard but that text isn't retrieved by the get_text() method.
> 
> What am I doing wrong?  Virtually nothing has changed in this part of
> my code between gtk+ 2 and gtk+ 3, just the import and gtk to Gtk and
> a couple of enum formats.
> 
You're creating a TextBuffer and a TextView, but I don't see any code 
that ties them together. How would the view know to display the contents 
of the buffer?