如何使用 Gtk 3 在盒子中强制小部件的大小
How force the widget's size in a box with Gtk 3
我正在制作一个程序,该程序应该有一个主体 textview(请参阅 textview),用户应该在其中写入,还有一个控制台,其中程序应该 return 错误消息。所以控制台应该比文本编辑器少。
window 顶部还有一个工具栏,允许用户使用我程序的工具来编辑文本。
为此,我创建了一个全局框,其中使用 pack_start
属性 (self.global_box.pack_start(self.console))
)
放置了工具栏、textview 和控制台(这也是一个 textview 对象)
工具栏放置正确(我放了一个按钮,所以它可以有一个长度,并且有效),但 space 的其余部分均匀通过控制台和文本视图,而我希望控制台只占用我 window.
的一小部分 space
我怎样才能让我的控制台只占用我的 window 的 20 个像素?或者只为 textview 设置我的 window 的百分比?
非常感谢
我
PS: here is a screenshot of the window, so you can have an idea of what I'm talking about.
编辑:
感谢您的回复,问题看起来是一样的,但解决方案对我不起作用。
这是代码:
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self,title='Text editor')
self.maximize()
self.global_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.global_box)
self.create_textview()
self.create_console()
def create_textview(self):
self.scrolled_window = Gtk.ScrolledWindow()
self.scrolled_window.set_hexpand(True)
self.scrolled_window.set_vexpand(True)
self.global_box.pack_start(self.scrolled_window, True, True, 0)
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
self.scrolled_window.add(self.textview)
self.tag_found = self.textbuffer.create_tag('found',background='yellow')
def create_console(self):
self.console_scrolled_window = Gtk.ScrolledWindow()
self.console_scrolled_window.set_hexpand(True)
self.console_scrolled_window.set_vexpand(True)
self.global_box.pack_start(self.console_scrolled_window, False, False, 0) #HERE /!\ the first one is expand argument, second one is irrelevant (the fill one)
self.console = Gtk.TextView()
self.console_scrolled_window.add(self.console)
self.console.set_editable(False)
问题出在此处有评论的那一行。
您正在将 set_hexpand
和 set_vexpand
设置为 True
,这意味着它们将展开。删除那些,它会正常工作。
我正在制作一个程序,该程序应该有一个主体 textview(请参阅 textview),用户应该在其中写入,还有一个控制台,其中程序应该 return 错误消息。所以控制台应该比文本编辑器少。
window 顶部还有一个工具栏,允许用户使用我程序的工具来编辑文本。
为此,我创建了一个全局框,其中使用 pack_start
属性 (self.global_box.pack_start(self.console))
)
工具栏放置正确(我放了一个按钮,所以它可以有一个长度,并且有效),但 space 的其余部分均匀通过控制台和文本视图,而我希望控制台只占用我 window.
的一小部分 space
我怎样才能让我的控制台只占用我的 window 的 20 个像素?或者只为 textview 设置我的 window 的百分比?
非常感谢 我
PS: here is a screenshot of the window, so you can have an idea of what I'm talking about.
编辑:
感谢您的回复,问题看起来是一样的,但解决方案对我不起作用。
这是代码:
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self,title='Text editor')
self.maximize()
self.global_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.global_box)
self.create_textview()
self.create_console()
def create_textview(self):
self.scrolled_window = Gtk.ScrolledWindow()
self.scrolled_window.set_hexpand(True)
self.scrolled_window.set_vexpand(True)
self.global_box.pack_start(self.scrolled_window, True, True, 0)
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
self.scrolled_window.add(self.textview)
self.tag_found = self.textbuffer.create_tag('found',background='yellow')
def create_console(self):
self.console_scrolled_window = Gtk.ScrolledWindow()
self.console_scrolled_window.set_hexpand(True)
self.console_scrolled_window.set_vexpand(True)
self.global_box.pack_start(self.console_scrolled_window, False, False, 0) #HERE /!\ the first one is expand argument, second one is irrelevant (the fill one)
self.console = Gtk.TextView()
self.console_scrolled_window.add(self.console)
self.console.set_editable(False)
问题出在此处有评论的那一行。
您正在将 set_hexpand
和 set_vexpand
设置为 True
,这意味着它们将展开。删除那些,它会正常工作。