[英]Python autoscroll in gtk textview widget
I just have a new project, i just want to learn more about gtk, threads, sockets and classes, so i decided to develop a simple IRC client.我刚刚有一个新项目,我只是想了解更多关于 gtk、线程、sockets 和类的信息,所以我决定开发一个简单的 IRC 客户端。
I have some code but i can not find a solution for this:我有一些代码,但我找不到解决方案:
How do i forbid horizontal scrolling in the textview widget of gtk, and also automatically scroll the textview widget to the bottom?如何在 gtk 的 textview 小部件中禁止水平滚动,并自动将 textview 小部件滚动到底部?
thanks in advance!提前致谢!
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Pango
class TextViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TinyIRC")
self.set_default_size(700, 500)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_chatbox()
self.create_entry()
def create_chatbox(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 3, 1)
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
start_iter = self.textbuffer.get_start_iter()
self.textbuffer.insert(start_iter, "This is some text ")
self.textbuffer.insert_markup(self.textbuffer.get_end_iter(), "<b>and some bold text</b>", -1)
scrolledwindow.add(self.textview)
def create_entry(self):
self.entry_box = Gtk.Entry()
self.grid.attach(self.entry_box, 0, 2 ,3 ,1)
self.entry_box.connect("activate", self.enter_pressed)
def enter_pressed(self, widget):
given_msg=self.entry_box.get_text()
send_msg=given_msg
print(given_msg)
end_iter = self.textbuffer.get_end_iter()
self.textbuffer.insert(end_iter, send_msg+'\n')
self.entry_box.set_text("")
win = TextViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Using ScrolledWindow.set_policy()
, you can control whether it scrolls horizontally or not.使用
ScrolledWindow.set_policy()
,您可以控制它是否水平滚动。 If you want it not to scroll horizontally, you would add this line right after creating scrolledwindow
:如果您不希望它水平滚动,您可以在创建
scrolledwindow
后立即添加此行:
scrolledwindow.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
The first argument of set_policy()
controls the horizontal scrolling, and the second controls the vertical scrolling. set_policy()
的第一个参数控制水平滚动,第二个控制垂直滚动。 Setting the horizontal scrolling to Gtk.PolicyType.NEVER
makes sure that scrolledwindow
never scrolls horizontally.将水平滚动设置为
Gtk.PolicyType.NEVER
确保scrolledwindow
永远不会水平滚动。
To scroll to the end after inserting all the text, you can create a mark at the end, and use textbuffer.scroll_mark_onscreen()
to scroll down to the end.要在插入所有文本后滚动到末尾,可以在末尾创建一个标记,并使用
textbuffer.scroll_mark_onscreen()
向下滚动到末尾。 Here is what that would look like:这就是它的样子:
# Create a mark at the end of all the text
mark = self.textbuffer.create_mark("end", self.textbuffer.get_end_iter(), False)
# Scroll so we can see the end mark
self.textview.scroll_mark_onscreen(mark)
When we apply these principles to your code, we get something like this (note that I insert much more text so that you can see how the scrolling works):当我们将这些原则应用于您的代码时,我们会得到类似这样的结果(请注意,我插入了更多文本,以便您可以看到滚动是如何工作的):
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Pango
class TextViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TinyIRC")
self.set_default_size(700, 500)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_chatbox()
self.create_entry()
def create_chatbox(self):
scrolledwindow = Gtk.ScrolledWindow()
# Make sure that the window doesn't scroll horizontally
scrolledwindow.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 3, 1)
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
start_iter = self.textbuffer.get_start_iter()
self.textbuffer.insert(start_iter, (("This is some text"*10) + "\n")*200)
end_iter = self.textbuffer.get_end_iter()
self.textbuffer.insert(end_iter, "This should be the end.")
scrolledwindow.add(self.textview)
# Create a mark at the end, and scroll down to it
mark = self.textbuffer.create_mark("end", self.textbuffer.get_end_iter(), False)
self.textview.scroll_mark_onscreen(mark)
def create_entry(self):
self.entry_box = Gtk.Entry()
self.grid.attach(self.entry_box, 0, 2 ,3 ,1)
self.entry_box.connect("activate", self.enter_pressed)
def enter_pressed(self, widget):
given_msg=self.entry_box.get_text()
send_msg=given_msg
print(given_msg)
end_iter = self.textbuffer.get_end_iter()
self.textbuffer.insert(end_iter, send_msg+'\n')
self.entry_box.set_text("")
win = TextViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.