亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python GTK 3+:如何在組合框小部件中居中文本?

Python GTK 3+:如何在組合框小部件中居中文本?

慕田峪7331174 2021-09-11 13:22:15
所以我有一個組合框,我想將內部文本居中(現在它都被推到了左邊)。但是,我似乎無法找到執行此操作的函數,也無法通過 CSS 觸及它。有什么建議嗎?謝謝。
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

我采用了流行的 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()



查看完整回答
反對 回復 2021-09-11
  • 1 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號