我有这个代码在scrolledwindow中创建一个textview。我想要一行说“Hello World”,作为第一行出现在textview中。此外,如何将此线固定在顶部,即使窗口向下滚动也应显示。此行的位置应该是固定的,即使在textview中输入n行或向下滚动到底部也可以看到它
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()
答案 0 :(得分:1)
首先在滚动窗口上方添加gtk.Entry控件。这将模拟将始终显示的文本视图的第一行。
添加信号处理程序,按下时将焦点从输入控件移动到文本视图输入键或光标向下。
添加信号处理程序,如果您在第一行上,则在向上按光标时将焦点从文本视图移动到输入控件。