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 7811809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:18:11+00:00 2026-06-02T04:18:11+00:00

I know this is not specific to tk, but rather a more general problem

  • 0

I know this is not specific to tk, but rather a more general problem with ruby DSLs and instance_eval, but here is my question, if I want to use the DSL for tk, I can’t figure out how to make certain things work. For example, if I want to call a instance method by pressing a button, this will not work, because it thinks I am trying to call a method with the same name in the Tk parent class (i.e. Tk::Button) as in the below code:

require 'tk'
class MyApp
  def initialize
    @root = TkRoot.new
    TkFrame.new {|f|
      TkButton.new(f) {
        text "Press Me"
        command proc {do_something()}
        pack
      }
      pack
    }
  end
  def do_something
    puts "Hello!"
  end
  def run
    Tk.mainloop
  end
end

MyApp.new.run

If I rewrite it not using the DSL, I can avoid this issue, but I prefer the DSL for various reasons:

class MyApp
  def initialize
    @root = TkRoot.new
    f = TkFrame.new
    TkButton.new(f, text: "Press Me", command: proc {do_something()}).pack
    f.pack
  end
  def do_something
    puts "Hello!"
  end
  def run
    Tk.mainloop
  end
end

MyApp.new.run

The same is true for instance variables of the MyApp class. Is there any way arround this?

  • 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-06-02T04:18:13+00:00Added an answer on June 2, 2026 at 4:18 am

    proc { do_something } is probably being evaluated in the context of the TkButton instance. That’s probably how you can call text, command and pack while inside the block.

    This effectively means self is not the MyApp instance anymore; it was changed to the TkButton instance.

    Try this:

    def initialize
      my_app = self  # self is the MyApp instance here
      @root = TkRoot.new
      TkFrame.new do |f|
        TkButton.new f do
          text "Press Me"
          command proc { my_app.do_something }
          pack
        end
        pack
      end
    end
    

    Since blocks are closures, the my_app local variable will be available to the block.

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

Sidebar

Related Questions

This question was asked already here , but rather than answering the specific question,
I know this is not programming related, but I have a scenario. Each video
Hey. I know this is not a 'refactor my code' site but I made
I know this question is not really important.. however I've been wondering: Which of
I know this might sounds not reasonable, and might be a bad design. But
I know this probably really simple but Im not sure what im doing wrong...
I know this sort of code is not best practice, but nevertheless in certain
I know this is possible but I'm not really sure where to start. Has
I know this is simple but I am not able to get it right
I have asked aqbout timezones and date/time before but this is a more specific

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.