python - gtk.ScrolledWindow 具有 gtk.TextView 显示固定的第一行

标签 python textview pygtk

我有这段代码,可以在滚动窗口中创建一个 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()

最佳答案

首先在滚动窗口上方添加 gtk.Entry 控件。这将模拟始终显示的 TextView 的第一行。

添加信号处理程序,在按下例如时将焦点从输入控件移动到 TextView 。按 Enter 键或向下移动光标。

添加信号处理程序,如果您位于第一行,则在向上按光标时将焦点从 TextView 移动到输入控件。

关于python - gtk.ScrolledWindow 具有 gtk.TextView 显示固定的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22984369/

相关文章:

python - 没有循环的多个 numpy 点产品

java - 执行 Html.fromHtml 后更改 TextViews 中 html 链接的样式

java - 如何在抽屉导航标题中更改 TextView 的文本?

python - 为什么 PyGtk 中的 set_model 方法将第一列的值复制到第三列?

python - 如何使用 SQLAlchemy + PostgreSQL 删除自定义类型?

python - 我无法使用 Python 在 Gmail 中搜索已发送的电子邮件

android - 使用按钮更改 listView 上的文本颜色

python - 如何在 pyGTK 中搜索 gtk.ListStore 并删除元素?

python - GtkLabel 对齐和填充

python - Pandas One 热编码 : Bundling together less frequent categories