PyGobject(八十三)Gtk.SizeGroup

本文介绍GtkSizeGroup组件的使用方法,通过Python示例代码展示如何将一组小部件的高度或宽度统一,实现美观一致的界面布局。

Gtk.SizeGroup

将一组小部件组织到一起,使它们拥有相同的高度或者宽度,由set_mode(Gtk.SizeGroupMode)来设置

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew (mode)
add_widget (widget)
get_ignore_hidden ()
get_mode ()
get_widgets ()
remove_widget (widget)
set_ignore_hidden (ignore_hidden)
set_mode (mode)

Virtual Methods

Properties

NameTypeFlagsShort Description
ignore-hiddenboolr/w/enIf True, unmapped widgets are ignored when determining the size of the group
modeGtk.SizeGroupModer/w/enThe directions in which the size group affects the requested sizes of its component widgets

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/21
# section 131

# 
# author: xiaosanyu
# website: yuxiaosan.tk \
#          http://blog.youkuaiyun.com/a87b01c14
# created: 16/7/21

TITLE = "SizeGroup"
DESCRIPTION = """
GtkSizeGroup provides a mechanism for grouping a number of
widgets together so they all request the same amount of space.
This is typically useful when you want a column of widgets to
have the same size, but you can't use a GtkTable widget.

Note that size groups only affect the amount of space requested,
not the size that the widgets finally receive. If you want the
widgets in a GtkSizeGroup to actually be the same size, you need
to pack them in such a way that they get the size they request
and not more. For example, if you are packing your widgets
into a table, you would not include the GTK_FILL flag.
"""
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkPixbuf, GLib

color_options = ["Red", "Green", "Blue"]

dash_options = ["Solid", "Dashed", "Dotted"]

end_options = ["Square", "Round", "Double Arrow"]


class SizeGroupWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="SizeGroup demo")
        self.set_resizable(False)
        vbox = Gtk.VBox(spacing=5)
        self.add(vbox)
        vbox.set_border_width(5)

        size_group = Gtk.SizeGroup.new(Gtk.SizeGroupMode.HORIZONTAL)

        # Create one frame holding color options
        frame = Gtk.Frame.new("Color Options")
        vbox.pack_start(frame, True, True, 0)

        grid = Gtk.Grid()
        grid.set_border_width(5)
        grid.set_row_spacing(5)
        grid.set_column_spacing(10)
        frame.add(grid)

        self.add_row(grid, 0, size_group, "_Foreground", color_options)
        self.add_row(grid, 1, size_group, "_Background", color_options)

        # And another frame holding line style options
        frame = Gtk.Frame.new("Line Options")
        vbox.pack_start(frame, False, False, 0)

        grid = Gtk.Grid()
        grid.set_border_width(5)
        grid.set_row_spacing(5)
        grid.set_column_spacing(10)
        frame.add(grid)

        self.add_row(grid, 0, size_group, "_Dashing", dash_options)
        self.add_row(grid, 1, size_group, "_Line ends", end_options)

        # And a check button to turn grouping on and off
        check_button = Gtk.CheckButton.new_with_mnemonic("_Enable grouping")
        vbox.pack_start(check_button, False, False, 0)

        check_button.set_active(True)
        check_button.connect("toggled", self.toggle_grouping, size_group)

    @staticmethod
    def create_combo_box(strings):
        combo_box = Gtk.ComboBoxText()
        for s in strings:
            combo_box.append_text(s)

        combo_box.set_active(0)

        return combo_box

    def add_row(self, grid, row, size_group, label_text, options):
        label = Gtk.Label.new_with_mnemonic(label_text)
        label.set_halign(Gtk.Align.START)
        label.set_valign(Gtk.Align.BASELINE)
        label.set_hexpand(True)
        grid.attach(label, 0, row, 1, 1)

        combo_box = self.create_combo_box(options)
        label.set_mnemonic_widget(combo_box)
        combo_box.set_halign(Gtk.Align.END)
        combo_box.set_valign(Gtk.Align.BASELINE)
        size_group.add_widget(combo_box)
        grid.attach(combo_box, 1, row, 1, 1)

    @staticmethod
    def toggle_grouping(check_button, size_group):
        # GTK_SIZE_GROUP_NONE is not generally useful, but is useful
        # here to show the effect of GTK_SIZE_GROUP_HORIZONTAL by
        # contrast.
        if check_button.get_active():
            new_mode = Gtk.SizeGroupMode.HORIZONTAL
        else:
            new_mode = Gtk.SizeGroupMode.NONE

        size_group.set_mode(new_mode)


def main():
    win = SizeGroupWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()





代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728

<think>好的,我现在需要帮助用户解决配置过程中出现的找不到Python GObject的问题。用户提供的错误信息是“configure error could not find python gobject pygobject-3.0”,我需要先理解这个错误的原因,然后给出解决方案。 首先,我应该回忆一下Python GObject的作用。GObject是GLib的类型系统,用于GNOME项目的对象模型,而PyGObjectPython的绑定,允许Python使用GObject库。当配置过程中提示找不到pygobject-3.0,通常是因为系统没有安装相应的开发包或者Python模块。 接下来,我需要考虑不同的操作系统环境,比如常见的Ubuntu/Debian、Fedora、Arch等,因为安装包的命令可能不同。用户可能使用的是基于Debian的系统,所以先列出Ubuntu的解决方案,然后再扩展其他发行版。 然后,检查是否安装了python3-gi或者python-gobject包。在Ubuntu中,可能需要安装python3-dev和gcc等编译工具,以及libgirepository1.0-dev。此外,通过pip安装PyGObject也是一种方法,但需要确保依赖项已安装。 另外,用户可能缺少pkg-config的配置,因为很多构建系统使用pkg-config来检测库的存在。需要确认pkg-config是否能找到pygobject-3.0,可以通过运行pkg-config --exists pygobject-3.0来检查,如果返回错误,说明没有正确安装。 还需要考虑Python环境的问题,比如是否使用了虚拟环境,或者是否安装了多个Python版本导致路径错误。这时候可能需要指定Python的路径或者重新安装PyGObject到正确的位置。 最后,建议用户在安装完成后重新运行configure,如果问题依旧,可能需要检查日志文件获取更多信息,或者寻求社区的帮助。同时,引用中的内容可能涉及其他依赖问题,比如GLib的废弃函数警告,但当前问题主要集中在PyGObject的缺失,所以暂时不需要处理这些引用。</think>### 解决方案:配置错误 "Could not find Python GObject (pygobject-3.0)" #### 问题原因 此错误表示系统缺少`pygobject-3.0`开发依赖,或构建工具未正确检测到其路径。PyGObjectPython 与 GLib/GObject 集成的关键组件[^1][^3]。 --- #### 分步解决方法 1. **安装 PyGObject 开发包** **Ubuntu/Debian**: ```bash sudo apt-get install python3-gi python3-dev gir1.2-gtk-3.0 libgirepository1.0-dev gcc ``` **Fedora**: ```bash sudo dnf install pygobject3 python3-devel gcc gobject-introspection-devel ``` **Arch Linux**: ```bash sudo pacman -S python-gobject gobject-introspection gcc ``` 2. **通过 pip 安装 PyGObject** 若系统包管理器未提供兼容版本,可补充安装: ```bash pip3 install pygobject ``` 3. **验证 pkg-config 路径** 运行以下命令检查是否返回有效路径: ```bash pkg-config --cflags --libs pygobject-3.0 ``` 若无输出,需确认开发包是否安装正确[^2]。 4. **重新生成配置缓存** 删除`configure`生成的临时文件后重试: ```bash make clean autoreconf -fvi ./configure ``` --- #### 常见问题补充 - **虚拟环境问题** 若使用虚拟环境,需确保在激活环境后安装`pygobject`,或通过`--system-site-packages`参数继承系统包。 - **多版本 Python 冲突** 明确指定 Python 解释器路径: ```bash PYTHON=/usr/bin/python3.10 ./configure ``` --- #### 验证安装 创建测试脚本`test_gi.py`: ```python import gi gi.require_version("GLib", "2.0") from gi.repository import GLib print(GLib.get_user_name()) ``` 执行若输出当前用户名,则环境正常。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值