TkDialog question

S

Stephen Kellett

Hi Folks.

I have a TkDialog that I just want to display and then get the answer
from it. I was wondering if it was possible to do this without deriving
from the class. Its a simple dialog box with a Yes/No question.

ret = TkDialog.new('title'=>'Confirm Exit',
'message'=>'Are you sure you want to exit?',
'buttons'=>['Yes', 'No'],
'default'=>1,
'bitmap'=>'question')

How do I get the answer? Did the user press Yes or No?

I've tried to find the answer to this by searching all the ruby examples
on my machine and using Google. I'm either looking for the wrong thing,
looking in the wrong place, whatever I can't work out how to do this.

Hoping someone can help me.

Stephen
 
S

Stephen Kellett

Stephen Kellett said:
ret = TkDialog.new('title'=>'Confirm Exit',
'message'=>'Are you sure you want to exit?',
'buttons'=>['Yes', 'No'],
'default'=>1,
'bitmap'=>'question')

How do I get the answer? Did the user press Yes or No?

Solved the problem.

ret.value() will return the index of the button pressed. In the example
above that is:
Yes == 0
No == 1

So the code would be:

If ret.value() == 0 # user pressed Yes
TkRoot.destroy();
end

You can also use ret.name() to get the name of the button pressed.

Stephen
 
E

email55555

FYI.
There maybe a bug for dialog.name.
If a dialog does not show up yet, the value will be nil, but calling
name will give you exception.

Example:
dialog = TkDialog2.new:)title=>'Hello', :message=>'Exit?',
:buttons=>['Yes', 'No'], :default=>1)
# TkDialog2 will not showing at initialize time

puts dialog.value # print nil
puts dialog.name # exception...

My suggestion is modify dialog.rb on the line # 202
def name
@buttons[@val]
end

by

def name
@val.nil? ? nil : @buttons[@val]
end
 
H

Hidetoshi NAGAI

From: (e-mail address removed)
Subject: Re: TkDialog question
Date: Thu, 15 Sep 2005 00:21:34 +0900
Message-ID: said:
There maybe a bug for dialog.name.
If a dialog does not show up yet, the value will be nil, but calling
name will give you exception.

Thank you for your report. I'll fix it on CVS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,514
Latest member
AdeleGelle

Latest Threads

Top