【发布时间】:2020-08-06 02:38:43
【问题描述】:
我有一个Gtk.Button,我想减少按钮内部的左右内边距,所以按钮的label显示在左右两边的边距更小。
我可以使用...增加边距
label = button.get_child()
label.set_margin_start(50)
label.set_margin_end(50)
但我无法减少边距。将边距设置为零无效...
label = button.get_child()
label.set_margin_start(0)
label.set_margin_end(0)
我还尝试获取样式上下文,并读取边距属性...
label = button.get_child()
style_context = label.get_style_context()
margin = style_context.get_margin(Gtk.StateFlags.NORMAL)
print('left = %s' % margin.left)
print('right = %s' % margin.right)
print('top = %s' % margin.top)
print('bottom = %s' % margin.bottom)
但是所有边都已经为零了……
left = 0
right = 0
top = 0
bottom = 0
如何减少按钮内部文本的左右填充?
编辑:我在下面添加了测试代码。
basic_window.py
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib
from gi.repository import Gtk
class MainWindowHandlers(Gtk.Window):
def on_window_destroy(self, *args):
print('Exit')
GLib.idle_add(Gtk.main_quit)
def on_clicked_button_1(self, button):
print('----------------------------------')
print('Button 1 Clicked')
print('----------------------------------')
# Get the child label.
label = button.get_child()
# Set the child's margins to 30. They expand.
label.set_margin_start(20)
label.set_margin_end(20)
# Investigate the child's style context.
style_context = label.get_style_context()
margin = style_context.get_margin(Gtk.StateFlags.NORMAL)
print('left = %s' % margin.left)
print('right = %s' % margin.right)
print('top = %s' % margin.top)
print('bottom = %s' % margin.bottom)
print('----------------------------------')
print()
def on_clicked_button_2(self, button):
print('----------------------------------')
print('Button 2 Clicked')
print('----------------------------------')
# Get the child label.
label = button.get_child()
# Set the child's margins to 0.
# They stay the same size.
# This does not reduce the left/right padding as I expect.
label.set_margin_start(0)
label.set_margin_end(0)
# Investigate the child's style context.
style_context = label.get_style_context()
margin = style_context.get_margin(Gtk.StateFlags.NORMAL)
print('left = %s' % margin.left)
print('right = %s' % margin.right)
print('top = %s' % margin.top)
print('bottom = %s' % margin.bottom)
print('----------------------------------')
print()
try:
builder = Gtk.Builder.new_from_file('basic_window.ui')
handlers = MainWindowHandlers()
builder.connect_signals(handlers)
window = builder.get_object('window')
window.show()
Gtk.main()
except Exception as exception:
print('Error: %s' % exception)
basic_window.ui
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="default_width">500</property>
<property name="default_height">300</property>
<signal name="destroy" handler="on_window_destroy" swapped="no"/>
<child>
<placeholder/>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button_2">
<property name="label" translatable="yes">Add 20 Pixels</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<signal name="clicked" handler="on_clicked_button_1" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button_1">
<property name="label" translatable="yes">Set to 0 Pixels</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<signal name="clicked" handler="on_clicked_button_2" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
【问题讨论】:
-
创建最小的工作代码,我们可以运行和测试问题。也许问题不是按钮,而是布局管理器,它可能会自动将小部件调整为窗口大小。