- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Menus & toolbars in Ruby GTK
In this part of the Ruby GTK programming tutorial, we will work with menus & toolbars.
A common part in a GUI application is a menubar. A menubar consists of objects called menus. Top-level menus have their labels on the menubar. The menus have menu items—commands that perform a specific action inside the application. Menus can also have submenus, which have their own menu items.
Simple menubar
In our first example, we will create a menubar with one file menu. The menu will have only one menu item. By selecting the item the application quits.
#!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This example creates a simple menubar. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui override_background_color :normal, Gdk::RGBA.new(0.2, 0.2, 0.2, 1) mb = Gtk::MenuBar.new filemenu = Gtk::Menu.new filem = Gtk::MenuItem.new "File" filem.set_submenu filemenu exit = Gtk::MenuItem.new "Exit" exit.signal_connect "activate" do Gtk.main_quit end filemenu.append exit mb.append filem vbox = Gtk::Box.new :vertical, 2 vbox.pack_start mb, :expand => false, :fill => false, :padding => 0 add vbox set_title "Simple menubar" signal_connect "destroy" do Gtk.main_quit end set_default_size 300, 200 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main
This is a small example with minimal menubar functionality.
mb = Gtk::MenuBar.new
A Gtk::MenuBar
widget is created. This is a container for the menus.
filemenu = Gtk::Menu.new filem = Gtk::MenuItem.new "File" filem.set_submenu filemenu
A toplevel Gtk::MenuItem
containing one Gtk::Menu
is created.
exit = Gtk::MenuItem.new "Exit" exit.signal_connect "activate" do Gtk.main_quit end filemenu.append exit
The Exit Gtk::MenuItem
is created and appended to the File Gtk::MenuItem
.
mb.append filem
A toplevel Gtk::MenuItem
is appended to the Gtk::MenuBar
widget.
vbox = Gtk::VBox.new false, 2 vbox.pack_start mb, false, false, 0
We put the menubar into the vertical box.

Submenu
The following example demonstrates how to create a submenu.
#!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This example shows a submenu. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui override_background_color :normal, Gdk::RGBA.new(0.2, 0.2, 0.2, 1) mb = Gtk::MenuBar.new filemenu = Gtk::Menu.new filem = Gtk::MenuItem.new "File" filem.set_submenu filemenu mb.append filem imenu = Gtk::Menu.new importm = Gtk::MenuItem.new "Import" importm.set_submenu imenu inews = Gtk::MenuItem.new "Import news feed..." ibookmarks = Gtk::MenuItem.new "Import bookmarks..." imail = Gtk::MenuItem.new "Import mail..." imenu.append inews imenu.append ibookmarks imenu.append imail filemenu.append importm exit = Gtk::MenuItem.new "Exit" exit.signal_connect "activate" do Gtk.main_quit end filemenu.append exit vbox = Gtk::Box.new :vertical, 2 vbox.pack_start mb, :expand => false, :fill => false, :padding => 0 add vbox set_title "Submenu" signal_connect "destroy" do Gtk.main_quit end set_default_size 350, 250 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main
A submenu is a menu inside another menu.
imenu = Gtk::Menu.new
A submenu is a Gtk::Menu
.
importm = Gtk::MenuItem.new "Import" importm.set_submenu imenu ... filemenu.append importm
It is a submenu of a menu item which belogs to the toplevel file menu.
inews = Gtk::MenuItem.new "Import news feed..." ibookmarks = Gtk::MenuItem.new "Import bookmarks..." imail = Gtk::MenuItem.new "Import mail..." imenu.append inews imenu.append ibookmarks imenu.append imail
The submenu has its own menu items.

Image menu
In the next example, we will further explore the menus. We will add images and accelerators to our menu items. Accelerators are keyboard shortcuts for activating a menu item.
#!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This example shows a menu with images, accelerators and a separator. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui override_background_color :normal, Gdk::RGBA.new(0.2, 0.2, 0.2, 1) mb = Gtk::MenuBar.new filemenu = Gtk::Menu.new filem = Gtk::MenuItem.new "File" filem.set_submenu filemenu agr = Gtk::AccelGroup.new add_accel_group agr newi = Gtk::ImageMenuItem.new :stock_id => Gtk::Stock::NEW, :accel_group => agr key, mod = Gtk::Accelerator.parse "N" newi.add_accelerator "activate", agr, key, mod, Gtk::AccelFlags::VISIBLE filemenu.append newi openm = Gtk::ImageMenuItem.new :stock_id => Gtk::Stock::OPEN, :accel_group => agr key, mod = Gtk::Accelerator.parse "O" openm.add_accelerator "activate", agr, key, mod, Gtk::AccelFlags::VISIBLE filemenu.append openm sep = Gtk::SeparatorMenuItem.new filemenu.append sep exit = Gtk::ImageMenuItem.new :stock_id => Gtk::Stock::QUIT, :accel_group => agr key, mod = Gtk::Accelerator.parse "Q" exit.add_accelerator "activate", agr, key, mod, :visible exit.signal_connect "activate" do Gtk.main_quit end filemenu.append exit mb.append filem vbox = Gtk::Box.new :vertical, 2 vbox.pack_start mb, :expand => false, :fill => false, :padding => 0 add vbox set_title "Image menu" signal_connect "destroy" do Gtk.main_quit end set_default_size 300, 200 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main
Our example shows a toplevel menu item with three menu items. Each of the menu items has a image and an accelerator. The accelerator for the quit menu item is active.
agr = Gtk::AccelGroup.new add_accel_group agr
To work with accelerators, we create a global Gtk::AccelGroup
object. It will be used later.
newi = Gtk::ImageMenuItem.new :stock_id => Gtk::Stock::NEW, :accel_group => agr key, mod = Gtk::Accelerator.parse "N" newi.add_accelerator "activate", agr, key, mod, Gtk::AccelFlags::VISIBLE filemenu.append newi
A Gtk::ImageMenuItem
is created. The image comes from the stock of images. We create also a Ctrl+N accelerator.
sep = Gtk::SeparatorMenuItem.new filemenu.append sep
These lines create a separator which is used to put menu items into logical groups.

Simple toolbar
Menus group commands that we can use in application. Toolbars provide a quick access to the most frequently used commands. Next we create a simple toolbar.
#!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This example creates a simple toolbar. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super init_ui end def init_ui toolbar = Gtk::Toolbar.new toolbar.set_toolbar_style Gtk::Toolbar::Style::ICONS newtb = Gtk::ToolButton.new :stock_id => Gtk::Stock::NEW opentb = Gtk::ToolButton.new :stock_id => Gtk::Stock::OPEN savetb = Gtk::ToolButton.new :stock_id => Gtk::Stock::SAVE sep = Gtk::SeparatorToolItem.new quittb = Gtk::ToolButton.new :stock_id => Gtk::Stock::QUIT toolbar.insert newtb, 0 toolbar.insert opentb, 1 toolbar.insert savetb, 2 toolbar.insert sep, 3 toolbar.insert quittb, 4 quittb.signal_connect "clicked" do Gtk.main_quit end vbox = Gtk::Box.new :vertical, 2 vbox.pack_start toolbar, :expand => false, :fill => false, :padding => 0 add vbox set_title "Toolbar" signal_connect "destroy" do Gtk.main_quit end set_default_size 300, 200 set_window_position :center show_all end end Gtk.init window = RubyApp.new Gtk.main
The example shows a toolbar and four tool buttons.
toolbar = Gtk::Toolbar.new
A Gtk::Toolbar
widget is created.
toolbar.set_toolbar_style Gtk::Toolbar::Style::ICONS
On the toolbar we only show icons.
newtb = Gtk::ToolButton.new :stock_id => Gtk::Stock::NEW
A Gtk::ToolButton
with an image from the stock is created. The image comes from the built-in stock of images.
sep = Gtk::SeparatorToolItem.new
This is a separator. It can be used to put toolbar buttons into logical groups.
toolbar.insert newtb, 0 toolbar.insert opentb, 1 ...
Toolbar buttons are inserted into the toolbar widget.

Undo redo
The following example demonstrates, how we can deactivate toolbar buttons on the toolbar. It is a common practise in GUI programming—for example the save button. If we save all changes of our document to the disk, the save button is deactivated in most text editors. This way the application indicates to the user that all changes are already saved.
#!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This example shows how to activate/deactivate a Gtk::ToolButton. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super @count = 2 init_ui end def init_ui toolbar = Gtk::Toolbar.new toolbar.set_toolbar_style Gtk::Toolbar::Style::ICONS @undo = Gtk::ToolButton.new :stock_id => Gtk::Stock::UNDO @redo = Gtk::ToolButton.new :stock_id => Gtk::Stock::REDO sep = Gtk::SeparatorToolItem.new quit = Gtk::ToolButton.new :stock_id => Gtk::Stock::QUIT toolbar.insert @undo, 0 toolbar.insert @redo, 1 toolbar.insert sep, 2 toolbar.insert quit, 3 @undo.signal_connect "clicked" do on_undo end @redo.signal_connect "clicked" do on_redo end quit.signal_connect "clicked" do Gtk.main_quit end vbox = Gtk::Box.new :vertical, 2 vbox.pack_start toolbar, :expand => false, :fill => false, :padding => 0 self.add vbox set_default_size 300, 200 set_window_position :center show_all set_title "Undo redo" signal_connect "destroy" do Gtk.main_quit end end def on_undo @count = @count - 1 if @count <= 0 @undo.set_sensitive false @redo.set_sensitive true end end def on_redo @count = @count + 1 if @count >= 5 @redo.set_sensitive false @undo.set_sensitive true end end end Gtk.init window = RubyApp.new Gtk.main
Our example creates undo and redo buttons from the GTK stock resources. After several clicks each of the buttons is deactivated. The buttons are grayed out.
@count = 2
The @count
variable decides which button is activated or deactivated.
@undo = Gtk::ToolButton.new :stock_id => Gtk::Stock::UNDO @redo = Gtk::ToolButton.new :stock_id => Gtk::Stock::REDO
We have two tool buttons: undo and redo tool buttons. The images come from the stock resources.
@undo.signal_connect "clicked" do on_undo end
Clicking on the undo button, we trigger the on_undo
method.
if @count <= 0 @undo.set_sensitive false @redo.set_sensitive true end
To activate or deactivate a widget, we use the set_sensitive
method

In this chapter of the Ruby GTK tutorial, we showed how to work with menus & toolbars.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论