2013-02-28 48 views
0

这里是我的工具按钮的代码(在工具栏):带图标和文字的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) 

下面是输出的截图:

Screennshot

正如你所看到的,没有标签 - 如何我可以设置要显示的标签吗?

这里是我的代码,对于那些有兴趣谁:

#! /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() 

回答

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),或去this page

0

我建议你使用格莱德界面编辑器,完成你想要做一个非常简单的方法是什么。一个工具按钮的标签是一个propiety这是在默认情况下,每当构件的标签是标签可见意味着它显示在图标下方的文本,而且即便如此,我们不应该放弃任何进一步指出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

+0

看到我刚刚发现的解决方案的答案。 – 2013-03-01 16:42:47

1

您可以使用:

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

虽然,你可能想考虑:

self.mainWindow.show_all() 

,将显示在主窗口小部件每()。通常在定义主UI之后使用,并希望使所有窗口小部件可见。

+0

我编辑了我的答案以显示我的所有代码 - 我确实运行了* .show_all(),但标签仍未显示。 – 2013-03-01 16:42:29