【问题标题】:Gtk.RadioToolButton with icon and text?带有图标和文本的 Gtk.RadioToolButton?
【发布时间】:2013-02-15 00:05:31
【问题描述】:

这是我的工具按钮代码(在工具栏中):

self.mainWindow.mainBox.mainToolbar.overviewRadio = Gtk.RadioToolButton(stock_id=Gtk.STOCK_ABOUT)
self.mainWindow.mainBox.mainToolbar.overviewRadio.set_label("Overview")
# self.mainWindow.mainBox.mainToolbar.overviewRadio.show_label() (No such function)
self.mainWindow.mainBox.mainToolbar.overviewRadio.connect("clicked", self.on_overviewRadio_clicked)

这是输出的截图:

如您所见,没有标签 - 如何设置要显示的标签?

这是我的代码,有兴趣的朋友可以看看:

#! /usr/bin/env python3

###    Copyright (c) 2013 - Marco Scannadinari

###    This program is free software: you can redistribute it and/or modify
###    it under the terms of the GNU General Public License as published by
###    the Free Software Foundation, either version 3 of the License, or
###    (at your option) any later version.
###
###    This program is distributed in the hope that it will be useful,
###    but WITHOUT ANY WARRANTY; without even the implied warranty of
###    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
###    GNU General Public License for more details.
###
###    You should have received a copy of the GNU General Public License
###    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# gcustomiser - A visual customiser for the GNOME desktop using GTK+.

from gi.repository import Gtk
import sys

class gcustomiser:
    def __init__(self):
        ## Main window
        self.mainWindow = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        self.mainWindow.set_size_request(512, -1)
        self.mainWindow.set_resizable(False)
        self.mainWindow.set_title("GNOME Customiser")
        self.mainWindow.connect("destroy", self.on_mainWindow_destroy)

        ## Main box
        self.mainWindow.mainBox = Gtk.VBox(
            homogeneous = False,
            spacing = 8)

        ## Toolbar
        self.mainWindow.mainBox.mainToolbar = Gtk.Toolbar()
        self.mainWindow.mainBox.mainToolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
        self.mainWindow.mainBox.mainToolbar.set_style(Gtk.ToolbarStyle.BOTH)

        ## Left toolbar separator
        self.mainWindow.mainBox.mainToolbar.leftSeparator = Gtk.SeparatorToolItem(draw = False)
        self.mainWindow.mainBox.mainToolbar.leftSeparator.set_expand(True)

        ## Overview toggle button
        self.mainWindow.mainBox.mainToolbar.overviewRadio = Gtk.RadioToolButton(Gtk.STOCK_ABOUT)
        self.mainWindow.mainBox.mainToolbar.overviewRadio.set_is_important(True)
        self.mainWindow.mainBox.mainToolbar.overviewRadio.set_label("Overview")
        self.mainWindow.mainBox.mainToolbar.overviewRadio.connect("clicked", self.on_overviewRadio_clicked)

        ## Basic settings toggle button
        self.mainWindow.mainBox.mainToolbar.basicRadio = Gtk.RadioToolButton(label = "Overview")
        self.mainWindow.mainBox.mainToolbar.basicRadio.set_label("Overview")
        a = self.mainWindow.mainBox.mainToolbar.basicRadio.get_label()
        print(a)
        ## Right toolbar separator
        self.mainWindow.mainBox.mainToolbar.rightSeparator = Gtk.SeparatorToolItem(
            draw = False)
        self.mainWindow.mainBox.mainToolbar.rightSeparator.set_expand(True)

        ## Add everything to self.mainWindow
        self.mainWindow.add(self.mainWindow.mainBox)

        self.mainWindow.mainBox.pack_start(
            self.mainWindow.mainBox.mainToolbar,
            expand = False,
            fill = True,
            padding = 0)

        self.mainWindow.mainBox.mainToolbar.add(self.mainWindow.mainBox.mainToolbar.leftSeparator)
        self.mainWindow.mainBox.mainToolbar.add(self.mainWindow.mainBox.mainToolbar.overviewRadio)
        self.mainWindow.mainBox.mainToolbar.add(self.mainWindow.mainBox.mainToolbar.rightSeparator)

    def on_mainWindow_destroy(self, *args):
        print("destroy: mainWindow")
        print("\nGoodbye.\n")

        Gtk.main_quit()

        sys.exit()

    def on_overviewRadio_clicked(self, *args):
        print("clicked: mainWindow.mainBox.mainToolbar.overviewRadio")

    def show_all(self):
        self.mainWindow.show_all()

window = gcustomiser()
window.show_all()

Gtk.main()

【问题讨论】:

    标签: python gtk radio-button toggle gnome


    【解决方案1】:

    问题是我没有设置工具栏样式。为此,我应该使用:

    self.mainWindow.mainBox.mainToolbar.set_style(Gtk.ToolbarStyle.*)
    

    如果我希望标签显示在图标的一侧,而不是在图标下方,我需要使单选按钮变得重要:

    self.mainWindow.mainBox.mainToolbar.set_style(Gtk.ToolbarStyle.BOTH_HORIZ)
    self.mainWindow.mainBox.mainToolbar.overviewRadio.set_is_important(True)
    

    有关Gtk.ToolbarStyles 的列表,请在 Python 解释器中使用dir(Gtk.ToolbarStyle),或转到this page

    【讨论】:

      【解决方案2】:

      你可以使用:

      self.mainWindow.mainBox.mainToolbar.overviewRadio.show_all()
      

      不过,您可能需要考虑:

      self.mainWindow.show_all()
      

      这将显示 mainWindow() 中的每个小部件。通常在您定义了主 UI 并希望使所有小部件可见之后使用。

      【讨论】:

      • 我已编辑答案以显示我的所有代码 - 我确实运行了 *.show_all(),但仍未显示标签。
      【解决方案3】:

      我建议您使用 Glade 界面编辑器并以一种非常简单的方式完成您想做的事情。 ToolButton 的标签是一个默认可见的属性,只要 widget 有标签标签就意味着它必须在图标下方显示文本,即使这样我们也应该没有进一步说明 show_labelshow_label 不是为 gtk.RadioToolButton 定义的函数。 此外,我建议你检查这些你离开这里的链接,你可能会有用

      http://valadoc.org/gtk+-3.0/Gtk.RadioToolButton.html.content.tpl%20 http://nullege.com/codes/search/gtk.RadioToolButton.set_label http://pascal.rigaud4.free.fr/Programmation/GTK/GTKMMDoc/GTKMM/www.gtkmm.org/docs/gtkmm-2.4/docs/reference/html/classGtk_1_1RadioToolButton.html

      【讨论】:

      • 查看我刚刚找到的解决方案的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多