19-Jun-15 Ruby GUI Toolkits. Available GUI toolkits The Tk GUI toolkit is a part of the Ruby distribution Tk is badly documented, does not support native.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books.
Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
App Inventor Barb Ericson July 3, 2013.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Automating Tasks With Macros
CS 580 Client-Server Programming Spring Semester, 2006 Doc 24 Some Ruby GUI 2 Apr 27, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney,
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
COMPSCI 101 Principles of Programming
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
PYTHON GUI PROGRAMMING
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1. Chapter 10 Managing and Printing Documents 3 Working with Files and Printing You can open multiple documents in Word. When multiple documents are.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Histo-Labels Easy to use and fun to setup. Create and edit all label menus to fulfill your slide labeling requirements. Print to any printer installed.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Lecture 10: Toolkits: Intrinsics, Callbacks, Resources, Widget hierarchies, Geometry management Brad Myers Advanced User Interface Software 1© 2013.
PERL TK. 4.Use an IDE 3. Use the documentation! 2. Experiment. 1.Learn the basics.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...
Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective.
Guide to Programming with Python
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Lesson 1 – Getting Started with App Inventor
Topics Graphical User Interfaces Using the tkinter Module
Event Loops and GUI Intro2CS – weeks
Graphical User Interfaces (GUIs)
Chapter 21 – Graphics/Tk Outline 21.1 Introduction 21.2 GD Module: Creating Simple Shapes 21.3 GD Module: Image Manipulation 21.4 Chart Module 21.5 Introduction.
Event loops 16-Jun-18.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Ruby GUI Toolkits 21-Sep-18.
GUI Using Python.
Graphical User Interfaces -- Introduction
Fundamentals of Python: From First Programs Through Data Structures
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Tkinter Widgets CS 260 Dick Steflik.
Event loops.
TKinter CS-360 Dick Steflik.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Topics Graphical User Interfaces Using the tkinter Module
Event loops 8-Apr-19.
Tonga Institute of Higher Education
Event loops.
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

19-Jun-15 Ruby GUI Toolkits

Available GUI toolkits The Tk GUI toolkit is a part of the Ruby distribution Tk is badly documented, does not support native widgets, and is generally regarded as ugly However, Tk is perfectly functional and easily available Other toolkits are FXRuby, GTK+, FLTK, Gt, probably others All have some significant disadvantages My less-than-extensive research (about an hour's worth) leads me to think that wxRuby2 is the best choice Advantages: Fairly stable, mature, cross-platform, native look-and-feel, good selection of widgets Disadvantage: C++ oriented The home page is at Download from RubyForge at

Modern event-driven programs Multiple sources of input mouse clicks keyboard timers external events In all modern languages, this is handled with an event loop Wait for event Dispatch event Quit

Java hides the event loop The event loop is built into Java GUIs Interacting with a GUI component (such as a button) causes an event to occur An Event is an object You create Listener s for interesting events The Listener gets the Event as a parameter In Ruby, the event loop is not built in However, Ruby GUI systems provide one—you just have to use it

“Hello world” in Tk require 'tk' root = TkRoot.new { title 'Our first Tk App' } label = TkLabel.new(root) do text 'Hello, world!' pack("padx" => 90) end Tk.mainloop All subsequent widgets will be contained by root The block uses instance_eval ; the title method belongs to the newly created instance pack is a geometry manager; it takes a hash as an argument The mainloop is the event handler

Geometry managers Geometry managers are like Java’s layout managers Ruby has three geometry managers: grid, place, and pack pack is like Java’s FlowLayout ; things are put into it in the order they are defined grid is like Java’s GridbagLayout, and is complex place specifies pixel coordinates, and is tedious pack is the best compromise for ordinary use

Allocation rectangle Every Ruby widget has an allocation rectangle, and is put somewhere inside that rectangle Here are the options for the hash argument to pack : The 'side' option tells where the allocation rectangle goes in the container-- 'top' (default), 'bottom', 'left', or 'right' The 'expand' option tells whether the allocation rectangle should be expanded to fill the remaining space--the value is either '0' (default, meaning don’t expand) or '1' The 'anchor' option tells where the widget goes in the allocation rectangle-- 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', or 'center' The 'fill' option tells how to stretch the widget to fill the container-- 'none' (default), 'x', 'y', or 'both' The 'padx' and 'pady' options specify margins The default is pixels You can suffix a number with a letter: i for inches, p for points, m for millimeters, or c for centimeters

Packing example I require 'tk' root = TkRoot.new() { title "Packing Example" } button = TkButton.new(root) { text "First, rightmost" } button.pack("side"=>"right", "fill"=>"y") entry = TkEntry.new(root).pack("side"=>"top", "fill"=>"x") entry.insert(0, "Entry on the top") label = TkLabel.new() { text "to the right" } label.pack("side"=>"right")

Packing example II image = TkPhotoImage.new('file'=>"background.gif", 'height'=>50) img_label = TkLabel.new(root) { image image }.pack("anchor"=>"e") text = TkText.new(root) { width 20; height 5 }.pack("side"=>"left") text.insert('end', "Left in canvas") TkMessage.new(root) { text "Message in the Bottom" }.pack("side"=>"bottom") Tk.mainloop()

Event handling example require 'tk' root = TkRoot.new() { title "Click the Button" } button = TkButton.new(root) { text "Hello..." command proc { puts "...world!" } } button.pack() Tk.mainloop() This brings up a window with a button labeled “ Hello... ” When the button is clicked, “...world! ” is printed to the console

RubyGems RubyGems is a standardized way of installing, upgrading, and removing Ruby packages It uses a centralized repository, so you don’t even have to know where to download packages from If you installed InstantRails, you already have RubyGems Example: C:\ruby\InstantRails\rails_apps>gem install wxruby2-preview Select which gem to install for your platform (i386-mswin32) 1. wxruby2-preview (i686-darwin8.8.2) 2. wxruby2-preview (i386-mswin32) 3. wxruby2-preview (powerpc-darwin7.9.0) 4. wxruby2-preview (i686-linux) 5. Skip this gem 6. Cancel installation > 1 Successfully installed wxruby2-preview i686-darwin8.8.2

“HelloWorld” in wxRuby2 require 'rubygems' require 'wx' include Wx # Extend the Wx::App class class HelloWorld < App # Define what the application is going to do when it starts def on_init # Make a frame with the title "Hello World" helloframe = Frame.new(nil, -1, "Hello World") # Put the text "Hello World" in that frame StaticText.new(helloframe,-1,"Hello World") # Make the window appear helloframe.show() end end # Make the program actually do it! HelloWorld.new.main_loop

Using wxRuby2 A tutorial for wxRuby2 is at Some of the examples don’t work as printed The following code, at the beginning of the file, seems to solve the problem: begin require 'wx' rescue LoadError => no_wx_err begin require 'rubygems' require 'wx' rescue LoadError raise no_wx_err end end include Wx

Using a BoxSizer # Code from previous slide goes here class MyApp < Wx::App def on_init() frame = Wx::Frame.new(nil, -1, 'Sizer happiness') frame.set_client_size(Wx::Size.new(200,200)) sizer = Wx::BoxSizer.new(Wx::VERTICAL) text = Wx::TextCtrl.new(frame, -1, 'Type in here', Wx::DEFAULT_POSITION, Wx::DEFAULT_SIZE, Wx::TE_MULTILINE) sizer.add(text, 1, Wx::GROW|Wx::ALL, 2) button = Wx::Button.new(frame, -1, 'Click on this') sizer.add(button, 0, Wx::ALIGN_RIGHT, 2) frame.set_sizer(sizer) frame.show() end end MyApp.new.main_loop()

Comments Ruby has no real “Ruby-like” GUI system Tk is the default if you are OK with just basic functionality and don’t care much about appearance You have several choices if you want something better than Tk This situation may change as Ruby becomes increasingly popular

The End