如何在自定义 Gtk 小部件中设置 minimum/preferred 大小
How to set minimum/preferred size in custom Gtk widget
我正在尝试实现自定义 Gtk 小部件,但我不知道如何请求特定的最小尺寸和首选尺寸。
在这里您可以看到我(尝试)要求最小尺寸为 300x300,首选尺寸为 500x500,但 Gtk 甚至从未调用任何这些函数并创建 200x200 window:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWidget(Gtk.DrawingArea):
# None of these are ever called
def do_get_preferred_width_for_height(self, height):
print('do_get_preferred_width_for_height')
return 300, 500
def do_get_preferred_height_for_width(self, width):
print('do_get_preferred_height_for_width')
return 300, 500
def do_get_preferred_size(self):
print('do_get_preferred_size')
min_size = Gtk.Requisition()
min_size.width = min_size.height = 300
pref_size = Gtk.Requisition()
pref_size.width = pref_size.height = 500
return min_size, pref_size
def do_size_request(self, requisition):
print('do_size_request')
requisition.width = 500
requisition.height = 500
# Except for these two
def do_get_request_mode(self):
print('do_get_request_mode')
return Gtk.SizeRequestMode.CONSTANT_SIZE
def do_draw(self, context):
print('Window size:', window.get_allocated_width(), window.get_allocated_height())
width = self.get_allocated_width()
height = self.get_allocated_height()
context.set_source_rgb(0, 1, 0)
context.rectangle(0, 0, width, height)
context.fill()
window = Gtk.Window()
window.add(MyWidget())
window.show_all()
window.connect('destroy', Gtk.main_quit)
Gtk.main()
# Output:
# do_get_request_mode
# do_get_request_mode
# Window size: 200 200
我做错了什么?
这里的问题(见)是do_get_preferred_height()
和do_get_preferred_width()
方法没有实现。这是您需要添加到 class:
的代码
def do_get_preferred_height(self):
print('get_preffered_height') # So you can see when it's called
return 300, 500
def do_get_preferred_width(self):
print('get_preffered_width') # So you can see when it's called
return 300, 500
这应该使 window 的初始尺寸为 500x500,最小尺寸为 300x300。
我正在尝试实现自定义 Gtk 小部件,但我不知道如何请求特定的最小尺寸和首选尺寸。
在这里您可以看到我(尝试)要求最小尺寸为 300x300,首选尺寸为 500x500,但 Gtk 甚至从未调用任何这些函数并创建 200x200 window:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWidget(Gtk.DrawingArea):
# None of these are ever called
def do_get_preferred_width_for_height(self, height):
print('do_get_preferred_width_for_height')
return 300, 500
def do_get_preferred_height_for_width(self, width):
print('do_get_preferred_height_for_width')
return 300, 500
def do_get_preferred_size(self):
print('do_get_preferred_size')
min_size = Gtk.Requisition()
min_size.width = min_size.height = 300
pref_size = Gtk.Requisition()
pref_size.width = pref_size.height = 500
return min_size, pref_size
def do_size_request(self, requisition):
print('do_size_request')
requisition.width = 500
requisition.height = 500
# Except for these two
def do_get_request_mode(self):
print('do_get_request_mode')
return Gtk.SizeRequestMode.CONSTANT_SIZE
def do_draw(self, context):
print('Window size:', window.get_allocated_width(), window.get_allocated_height())
width = self.get_allocated_width()
height = self.get_allocated_height()
context.set_source_rgb(0, 1, 0)
context.rectangle(0, 0, width, height)
context.fill()
window = Gtk.Window()
window.add(MyWidget())
window.show_all()
window.connect('destroy', Gtk.main_quit)
Gtk.main()
# Output:
# do_get_request_mode
# do_get_request_mode
# Window size: 200 200
我做错了什么?
这里的问题(见do_get_preferred_height()
和do_get_preferred_width()
方法没有实现。这是您需要添加到 class:
def do_get_preferred_height(self):
print('get_preffered_height') # So you can see when it's called
return 300, 500
def do_get_preferred_width(self):
print('get_preffered_width') # So you can see when it's called
return 300, 500
这应该使 window 的初始尺寸为 500x500,最小尺寸为 300x300。