带图标和文本的Gtk.RadioToolButton?
这是我在工具栏中用来创建工具按钮的代码:
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()
3 个回答
0
我建议你使用Glade这个界面编辑器,这样可以很简单地实现你想要的功能。一个ToolButton的标签是一个默认可见的属性,当这个widget有标签时,意味着它会在图标下方显示文字。即使如此,我们也不需要再设置其他的属性,比如show_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
1
你可以使用:
self.mainWindow.mainBox.mainToolbar.overviewRadio.show_all()
不过,你可能还想考虑:
self.mainWindow.show_all()
这个会显示mainWindow()中的每一个小部件。通常在你定义好主要的用户界面后使用,目的是让所有的小部件都变得可见。
0
问题在于我没有设置工具栏的样式。要做到这一点,我应该使用:
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.ToolbarStyle
样式,可以在Python解释器中使用dir(Gtk.ToolbarStyle)
,或者访问这个页面。