0

I have two problems regarding the TextView widget for PyGTK. How do I dictate the size of the TextView and how do I simply get and insert text (not just set text) into TextView ?

Below is my source code:

import sys

importStatus = False
output = None

try:
    import pygtk
    pygtk.require('2.0')
    import gtk
    importStatus = True

except ImportError:
    print "PyGTK module does not exist. Can't launch GUI !"
    print "Please download and install GTK and PyGTK."
    importStatus = False

if importStatus:

    class gtkGUI():

        def __init__(self):
            print "gtkGUI imported"
            self.startGUI()

        def startGUI(self):
            print "GUI Started"
            self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
            self.window.set_border_width(10)

            ## Buttons
            self.btn = gtk.Button("Press me !")

            ## Text View with frame wrapping
            self.page_size = gtk.Adjustment(lower=100, page_size=100)
            self.sw = gtk.ScrolledWindow(self.page_size)
            self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
            output = gtk.TextView()
            self.textbuffer = output.get_buffer()
            output.set_wrap_mode(gtk.WRAP_WORD)
            output.set_editable(False)
            self.sw.add(output)
            self.frame = gtk.Frame()
            self.frame.set_label(" Result: ")
            self.frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
            self.frame.add(self.sw)

            ## Packing widgets into window

            # Vertical box to contain all boxes
            self.vbox = gtk.VBox(homogeneous=False, spacing=0)

            # Output View
            self.vbox.pack_start(self.frame, expand=False, fill=False, padding=5)

            # Adding button to window
            self.box = gtk.HBox(homogeneous=False, spacing=0)
            self.box.pack_end(self.btn, expand=False, fill=False, padding=5)
            self.vbox.pack_start(self.box, expand=False, fill=False, padding=5)

            ## Presenting window
            self.window.add(self.vbox)
            self.window.show_all()
            gtk.main()
            self.insertText("helloworld")
            return None

        def insertText(self, text):
            if(output == None):
                print "Empty Output"
            else:
                print "inserting - " + output

Output when running the above code:

Empty Output

These codes above are the core codes that I have summarized from a bigger codebase containing the essentials to the problem.

Java has a "textarea.append("...");" and "textarea.getText()" for Swing. I can't find anything similar for the TextView in PyGTK.

thotheolh
  • 7,040
  • 7
  • 33
  • 49
  • I have figured how to insert text using 'self.buffer = self.output.get_buffer()' and then 'self.buffer.insert_at_cursor(text)'. – thotheolh Dec 09 '11 at 06:55
  • I managed to shift the size of the TextView by making the HBox that contains the TextView components to allow expand (expand=True) and fill (fill=True) but the fundamental question is how do I shift the size of the TextView components without relying on HBox or some layout ? – thotheolh Dec 09 '11 at 08:55

2 Answers2

3

In your code, output is a local variable inside the method, therefore is always None. Also, output is the TextView, you are interested in self.textbuffer.

With respect to layouts, this is handled with containers like HBox and VBox. If you want to set them manually, then you might want to use the not recommended special container GtkFixed.

The advantage of HBox (and VBox) is that allows the application to adapt itself to different sizes, font sizes, etc. You only have to define which is the behaviour they should have, but everything else will be adaptable in runtime.

gpoo
  • 8,408
  • 3
  • 38
  • 53
2

Surely you have read The Tutorial?

Instead of dictating the size of the TextView, you might dictate the (default) size of the whole window:

self.window.set_default_size(500, 800)

Appending is something I would have hoped to be more straightforward. What I've used is: Create an iterator to end of buffer, insert text there, scroll. Thus:

t = "text to append"
ti = self.mybuffer.get_end_iter()
self.mybuffer.insert(ti, t)
self.myview.scroll_to_iter(ti, 0.1)

Iterators also allow getting text. There's also some get functions in TextBuffer.

XTL
  • 851
  • 1
  • 8
  • 23