Building a multiple choice exam -Ruby Tk

134 views
Skip to first unread message

Nik

unread,
Feb 16, 2012, 10:19:31 AM2/16/12
to Tk Documentation and Resources
Hi,
Very new to Tk (pretty new to Ruby and programming as well, as will be
evident from the code below).

I think my problem revolves principally around the need to use global
variables for TkVariables.new.

I am trying to build a simple multiple choice exam using radio
buttons. I want to assign an "answer" value for each question which
ultimately gets pushed to an array that is compared with the answer
key (via the final_score method). This is straightforward using local
variables in a simple Ruby script, but given that the answer has to be
global (ie-$answer) in Tk, it changes every time a radio button is
pressed. Is there any way around this? Am I not understanding
something very clear (highly likely)?

Thanks for any and all feedback,
Nik

Here's the code:


require 'tk'
require 'tkextlib/tile'

#build an empty array to take answers provided
$answers =[]

#method to add answers provided to the empty array
def add_em
$answers.push($answer)
end

$answer = TkVariable.new
root = TkRoot.new {title "Q1"; geometry "+400+200"}

#the questions, answers provided via radio button press
#Q1
lbl = Tk::Tile::Label.new(root) {text "Nik did Grade 1 at:"}.grid
st_marys = Tk::Tile::RadioButton.new(root) {text "St. Mary's";
variable $answer; value 'A'}.grid
e_bilingue = Tk::Tile::RadioButton.new(root) {text "Ecole Bilingue";
variable $answer; value 'B'}.grid
st_lukes = Tk::Tile::RadioButton.new(root) {text "St. Luke's";
variable $answer; value 'C'}.grid
mdh = Tk::Tile::RadioButton.new(root) {text "MDH"; variable $answer;
value 'D'}.grid
school_but = Tk::Tile::Button.new(root) {text 'Next Q'; command
'add_em'}.grid

#Q2
lbl = Tk::Tile::Label.new(root) {text "MDH School Motto:"}.grid
lbe = Tk::Tile::RadioButton.new(root) {text "Lead By Example";
variable $answer; value 'A'}.grid
rts = Tk::Tile::RadioButton.new(root) {text "Ready To Serve"; variable
$answer; value 'B'}.grid
ro = Tk::Tile::RadioButton.new(root) {text "Reach Out"; variable
$answer; value 'C'}.grid
god = Tk::Tile::RadioButton.new(root) {text "In God We Serve";
variable $answer; value 'D'}.grid
motto_but = Tk::Tile::Button.new(root) {text 'Next Q'; command
'add_em'}.grid

#Q3
lbl = Tk::Tile::Label.new(root) {text "Funny Hair:"}.grid
mikec = Tk::Tile::RadioButton.new(root) {text "Mike C"; variable
$answer; value 'A'}.grid
mikeb = Tk::Tile::RadioButton.new(root) {text "Mike B"; variable
$answer; value 'B'}.grid
chrisw = Tk::Tile::RadioButton.new(root) {text "Chris Wright";
variable $answer; value 'C'}.grid
pauls = Tk::Tile::RadioButton.new(root) {text "Paul Spence"; variable
$answer; value 'D'}.grid
hair_but = Tk::Tile::Button.new(root) {text 'Next Q'; command
'add_em'}.grid

#button bound to method used to compare answers to answer key
score_but = Tk::Tile::Button.new(root) {text 'Final Score'; command
'final_score'}.grid

#compare answer array to answer key array
def final_score
key = %w[C A D]
score=0
i=0
$answers.each {|e|
if e == key[i]
score = score+1
end
i = i+1
}
puts score
end
Tk.mainloop
Reply all
Reply to author
Forward
0 new messages