Ruby/TK Question

K

Kujawa, Greg

I am new to Ruby and trying to create a basic application using Tk.
Unfortunately I'm also new to this as well :)

The application is for a survey. I have created an initial window where the
survey taker signs in. They select their name from a TkListbox object. When
they click on a TkButton object the sign in window exits and I record the
appropriate current selection of the listbox object.

My question is how would I go about creating a new window for taking survey
itself and still keep the saved selection of the listbox object? Is Tk
intended more for a single windowed application? I read about multi-tk and
was wondering if this might be the way to go. I want to create a new window
for the survey, record the takers responses and then clear their name off
the sign in list after they have completed things. That's why I want to
retain the sign in's selection for the lifetime of the application.

Here is my current code for the survey sign in below. Any pointers would be
appreciated!

require 'dbi'
require 'tk'

# connect to the SQL data source
dsn= 'DBI:ADO:provider=SQLOLEDB;Data Source=mobile_tech;Initial
Catalog=CoreVals;Persist Security Info=False;Trusted_Connection=Yes;'

DBI.connect(dsn) do |dbh|

# select all employees that haven't completed the survey
dbh.execute("SELECT First+' '+MI+' '+Last FROM TblEmployee where Done=0
order by Last, First") do |sth|

# populate the returned recordset into an array
$employee = sth.fetch_all
end
end

# create a new window for the sign in using Tk
@root = TkRoot.new() {title 'Core Values Survey'}

listViewFrm = TkFrame.new(@root).pack(
'side'=>'left',
'padx'=>10,
'pady'=>10,
'fill'=>'both')

# Create a scrollbar for the employee listbox
scrollbar = TkScrollbar.new(listViewFrm).pack(
'side'=>'right',
'fill'=>'y')

# Create the listbox to hold all of the employees
$empListbox = TkListbox.new(listViewFrm) {
width 25
}.pack(
'fill'=>'y',
'expand'=>'true')

# Link the listbox to scrollbar
$empListbox.yscrollbar(scrollbar)

# Populate the listbox control with the employee recordset
$employee.each { |emp|
$empListbox.insert('end', emp)
}

# Add the sign in button
button = TkButton.new(@root) { text "Sign me in!"
command proc {
indexstr = $empListbox.curselection.to_s;
indexnum = indexstr.to_i;
$taker=$employee[indexnum].to_s;
p $taker;
exit
}
}
button.pack("side"=>"right")

Tk.mainloop
 
M

Michael DeHaan

I'd consider hiding the root window after the input is provided, so
that it stays around but you can't see it. Tk can definitely support
multiple windows, add as many as you like.

I've only done Perl/Tk to this point, so I can't give you the
ruby-specific syntax...

--MPD
 
H

Hidetoshi NAGAI

Hi,

From: "Kujawa, Greg" <[email protected]>
Subject: Ruby/TK Question
Date: Fri, 5 Nov 2004 02:09:08 +0900
Message-ID: said:
The application is for a survey. I have created an initial window where the
survey taker signs in. They select their name from a TkListbox object. When
they click on a TkButton object the sign in window exits and I record the
appropriate current selection of the listbox object.

I recommend you to define a binding to the listbox for more
comfortable UI. ;-) For example,
----------------------------------------------------------
$empListbox..bind('Double-Button-1', proc{button.invoke})
----------------------------------------------------------
My question is how would I go about creating a new window for taking survey
itself and still keep the saved selection of the listbox object?

Please use the TkToplevel class. It creates a new window object.
Because the window depends on the root (or its parent) widget,
when the root widget is destroyed, the window is destroyed too.
If you want to hide the root widget, use {TkRoot/TkToplevel}#withdraw
and deiconify method.
# Add the sign in button
button = TkButton.new(@root) { text "Sign me in!"
command proc {
indexstr = $empListbox.curselection.to_s;
indexnum = indexstr.to_i;
$taker=$employee[indexnum].to_s;
p $taker;
exit

Probably, this 'exit' exits the application.
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top