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.