Python GTK 3+ : How to center text within Combobox widget?

标签 python gtk3

所以我有一个组合框,我想将内部文本居中(现在它都被推到左边)。但是,我似乎找不到执行此操作的函数,也似乎无法通过 CSS 来触及它。有什么建议吗?谢谢。

最佳答案

我采用了流行的 Gtk Combobox 示例并对其进行了修改以显示如何使文本居中:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ComboBoxWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="ComboBox Example")

        self.set_border_width(10)

        name_store = Gtk.ListStore(int, str)
        name_store.append([1, "Billy Bob"])
        name_store.append([11, "Billy Bob Junior"])
        name_store.append([12, "Sue Bob"])
        name_store.append([2, "Joey Jojo"])
        name_store.append([3, "Rob McRoberts"])
        name_store.append([31, "Xavier McRoberts"])

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)

        name_combo = Gtk.ComboBox.new_with_model_and_entry(name_store)
        name_combo.connect("changed", self.on_name_combo_changed)
        name_combo.set_entry_text_column(1)
        vbox.pack_start(name_combo, False, False, 0)

        country_store = Gtk.ListStore(str)
        countries = ["Austria", "Brazil", "Belgium", "France", "Germany",
        "Switzerland", "United Kingdom", "United States of America",
        "Uruguay"]
        for country in countries:
            country_store.append([country])

        country_combo = Gtk.ComboBox.new_with_model(country_store)
        country_combo.connect("changed", self.on_country_combo_changed)
        renderer_text = Gtk.CellRendererText()
        renderer_text.set_property("xalign", 0.51) #less than 0.51 has no affect
        country_combo.pack_start(renderer_text, True)
        country_combo.add_attribute(renderer_text, "text", 0)

        vbox.pack_start(country_combo, False, False, True)

        currencies = ["Euro", "US Dollars", "British Pound", "Japanese Yen",
        "Russian Ruble", "Mexican peso", "Swiss franc"]
        currency_combo = Gtk.ComboBoxText()
        currency_combo.set_entry_text_column(0)
        currency_combo.connect("changed", self.on_currency_combo_changed)
        for currency in currencies:
            currency_combo.append_text(currency)

        vbox.pack_start(currency_combo, False, False, 0)

        self.add(vbox)

    def on_name_combo_changed(self, combo):
        tree_iter = combo.get_active_iter()
        if tree_iter is not None:
            model = combo.get_model()
            row_id, name = model[tree_iter][:2]
            print("Selected: ID=%d, name=%s" % (row_id, name))
        else:
            entry = combo.get_child()
            print("Entered: %s" % entry.get_text())

    def on_country_combo_changed(self, combo):
        tree_iter = combo.get_active_iter()
        if tree_iter is not None:
            model = combo.get_model()
            country = model[tree_iter][0]
            print("Selected: country=%s" % country)

    def on_currency_combo_changed(self, combo):
        text = combo.get_active_text()
        if text is not None:
            print("Selected: currency=%s" % text)

win = ComboBoxWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

关于Python GTK 3+ : How to center text within Combobox widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678686/

相关文章:

python - 根据 Python/R 中的 ID 和日期将值复制到数据框中列的前 n 个单元格中

python - Google 云端硬盘在共享云端硬盘中创建新文件夹

c++ - C++ 中的 Gtk::TreeModelColumn/GType (gtkmm3)

compiler-errors - GTK +教程编译错误

python - 在带有菜单栏的窗口中使用 Gtk 播放视频

python-igraph 如何添加带权重的边?

python - pandas groupby 对象到数据框

python - 如何在 Python 中将变量传入和传出函数

c - 覆盖 TreeView 中使用的图标

python-2.7 - 如何在 Glade 中设置 GtkLinkBut​​ton 的标签?