Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:42:11+00:00 2026-05-30T08:42:11+00:00

The following code creates 3 buttons and adds a handler to each one. The

  • 0

The following code creates 3 buttons and adds a handler to each one. The Quit button works, the Give Up button produces an error NameError unknown option settings_change and suggests that an object has been deleted. Same with the Next button. The code works ok when I put the event handlers outside the class.

It turns out that if a callback is created by first doing something like next_note_proc = proc {next_note}, then in the button creation do command next_note_proc. Why does this work??

Why do the callbacks work differently when inside or outside the class?

require 'tk'
require 'tkextlib/tile'

class App

  def next_note
    puts "Got next note"
  end

  def settings_change
    puts "Got settings change"
  end

  def quit
    puts "Got exit"
    exit(1)
  end

  def initialize
    $number_correct = TkVariable.new;
    $mode = TkVariable.new

    @root = TkRoot.new {title "Music Training"}
    @content = Tk::Tile::Frame.new(@root) {padding "0 0 0 0"}.grid( :sticky => 'nsew')

    @a = Tk::Tile::Button.new(@content) {text 'Next'; command {next_note}}.grid( :column => 1, :row => 1, :sticky => 'w')
    @b = Tk::Tile::Button.new(@content) {text 'Give up'; command {settings_change}}.grid( :column => 2, :row => 1, :sticky => 'w')
    @c = Tk::Tile::Button.new(@content) {text 'Quit'; command {quit}}.grid( :column => 2, :row => 2, :sticky => 'w')
    TkWinfo.children(@content).each {|w| TkGrid.configure w, :padx => 0, :pady => 0}

    @c.bind("1") {quit}
    @a.bind("1") {next_note}
    @b.bind("1") {settings_change}

    puts "Starting up"
  end

   def run
     Tk.mainloop
   end  

end

the_app = App.new
the_app.run
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:42:12+00:00Added an answer on May 30, 2026 at 8:42 am

    Commands executed from buttons run in the global context, but settings_change, quit and next_note are in the context of the class. When you use the proc command it creates a new Proc object which calls the method, and which can be called from other contexts.

    The reason the quit command seems to work is probably because there is another quit command at the global scope that is getting called — it is almost certainly not calling the quit method of the App object. You can verify that by adding a print statement in the quit method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code creates a two radio buttons. Each option contains a date value
I created a grid of buttons. The following code creates the buttons and displays
I'm getting the following error whenever my code creates a DataTableReader from a valid
I have the following code to create two buttons (one on the left one
Suppose I have the following code which creates two radio buttons: <li id=foli517 class=
The following XAML code creates a button with a border as its content. The
I have the following code which creates a new Button with a ContextMenu with
I'm trying to create a universal handler for my buttons, but the following code
I have created an alert view with two buttons using the following code: UIAlertView
I have the following code that creates two objects (ProfileManager and EmployerManager) where the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.