Question

I have this code that creates a textview within scrolledwindow. I want a line say, "Hello World", to be appeared inside the textview as first line. Furthermore, how can I fix this line on top itself,i.e it should be displayed even when the window is scrolled down. The position of this line should be fixed,making it visible even after entering n number of lines in textview or scrolling all way to the bottom

import gtk
class scoreWindow:
   def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    #self.window.set_size_request(800, 200)
    self.window.set_resizable(False)
    self.window.set_title("Score card")
    self.window.set_position(gtk.WIN_POS_CENTER)
    self.vb=gtk.VBox()

    line="This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant.This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."

    self.go=gtk.Label("Textview\n")
    self.lalign = gtk.Alignment(0, 0, 0, 0)
    self.label_result = gtk.Label("  Title")
    #self.label_result.set_justify(gtk.JUSTIFY_LEFT)
    self.lalign.add(self.label_result)

    self.scrolled_window = gtk.ScrolledWindow()
    self.scrolled_window.set_border_width(10)
    self.scrolled_window.set_size_request(300, 300)

    self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
    self.scrolled_window.show()
    self.tv = gtk.TextView()
    self.tv.set_editable(1)
    self.tv.set_cursor_visible(1)
    self.tv.set_left_margin(30)
    textbuffer = self.tv.get_buffer()
    self.tv.show()
    textbuffer.set_text(line)

    self.scrolled_window.add_with_viewport(self.tv)


    self.vb.pack_start(self.lalign, False, False, 0)
    self.vb.pack_start(self.go, False, False, 0)
    self.vb.pack_start(self.scrolled_window, True, True, 0)
    color = gtk.gdk.color_parse('#FF8300')
    self.window.modify_bg(gtk.STATE_NORMAL, color)
    self.window.add(self.vb)
    self.window.show_all()

  def main(self):
    gtk.main()



if __name__ == "__main__":
hello = scoreWindow()
hello.main()
Was it helpful?

Solution

Start by adding a gtk.Entry control above the scrolled window. This will emulate the first line of your text view that will always show.

Add signal handlers that move focus from the entry control to the text view when pressing e.g. the enter key or cursor down.

Add signal handlers that move focus from the text view to the entry control when pressing cursor up if you're on the first line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top