Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

realize the insert function 1

Status
Not open for further replies.

waistcoat

Programmer
Aug 2, 2003
15
CN
hi,all
First,thanks very much for ulis's help that time.Now I can't realize the insert function.Can someone help me?
I want to insert a button in the desinged location and work for it a long time but fialed.
The program is bolow:
# proc bgerror (to see what happens behind the scene)
proc bgerror {args} { tk_messageBox -message "bgerror: $args" }
# proc forward
proc forward {} {
global wc x1 x2 y i bli
# type 1
lappend bli 1
# create label & entry
label $wc.labe$i -text "$i forward" -relief raised -bg navy -fg white
$wc create window $x1 $y -window $wc.labe$i -anchor n -tags item
entry $wc.entry$i
$wc create window $x2 $y -window $wc.entry$i -anchor n -tags item
# update globals
incr i
incr y 20
# debug
puts "forward: created label $i, entry $i"
}

# proc end
proc end {} {
global wc x1 x2 y i bli
# type 9
lappend bli 9
# create label
label $wc.labe$i -text "$i end" -relief raised
$wc create window $x1 $y -window $wc.labe$i -anchor n -tags item
# update globals
incr i
incr y 20
# debug
puts "end: created label $i"
}
# proc del
proc del {} {
global wc x1 x2 y i bli
# check if empty
if {$i == 1} { return; # no more labels }
# current row
incr i -1
# delete folowing type
set type [lindex $bli end]
if {[lsearch {1 2 3 4 5 6 8} $type] > -1} {
destroy $wc.labe$i
destroy $wc.entry$i
puts "delete: deleted label $i, entry $i"
} else {
destroy $wc.labe$i
puts "delete: deleted label $i"
}
# update globals
incr y -20
set bli [lrange $bli 0 end-1]
# debug
puts "delete: deleted label $i"
}
#proc insert {} {
}
#the main frame
set w .main
toplevel $w
#The left frame
set w1 $w.one
frame $w1 -width 200 -height 400 -bg grey50
button $w1.b2 -text "forward" -width 10 -command forward
button $w1.b10 -text "end" -width 10 -command end
pack $w1 -side left -fill x
pack $w1.b2 $w1.b10 -side top -expand yes -pady 2
#the middle frame
set w2 $w.two
frame $w2 -width 200 -height 50 -bg white
frame $w2.frame
frame $w2.bottom -width 200 -height 50 -bg blue
pack $w2 -side left -fill x
pack $w2.frame -side top
pack $w2.bottom -side bottom
#the midllemiddle frame
set wc $w2.frame.c
canvas $wc -scrollregion {0 0 200 2400} -width 200 -height 300 -relief sunken -borderwidth 2 -xscrollcommand "$w2.frame.hscroll set" -yscrollcommand "$w2.frame.vscroll set"
scrollbar $w2.frame.vscroll -command "$wc yview"
scrollbar $w2.frame.hscroll -orient horizon -command "$wc xview"
grid $wc -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
grid $w2.frame.vscroll -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
grid $w2.frame.hscroll -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
grid rowconfig $w2.frame 0 -weight 1 -minsize 0
grid columnconfig $w2.frame 0 -weight 1 -minsize 0
#middlebottom
button $w2.bottom.1 -text delete -width 5 -command del
button $w2.bottom.2 -text insert -width 5 -command insert
pack $w2.bottom.1 $w2.bottom.2 -side left -fill both -expand true

# labels x position
set x1 20
# entries x position
set x2 110
# next widget y position
set y 5
# next row
set i 1
# types list (1: forward, 9: end)
set bli {}
THANKS
 
I guess I don't understand what you want to do. I ran your script and, sure enough, the "insert" button is there but it doesn't do anything. The "forward" and "end" button work fine as well as the the "delete" button. Obviously, you have the "insert" button set to a non-existent command.

I gather your problem is writing the "insert" proc. What do you want it to do?

Bob Rashkin
rrashkin@csc.com
 
I want to insert a button after a button(for example,I thougt there need a button betwen 3 and 4). Now my time is limited,I have to better the interface,but I need the insert function.I am a novice in programing.
 
Once you've "packed" or "gridded" your buttons in the frame, I don't know of anyway to put something in between other than to "destroy" the blocking button and then pack in 2 more (the new one followed by the one that was already there). You can get the properties of the button you're going to destroy and use them when you bring it back.

Bob Rashkin
rrashkin@csc.com
 
Now,I list part of my program below:
proc end {} {
set w .main
set w2 $w.two
global c number x1 x2 y i i1 i2
global del1 ins1 ins2 ins4 y1
if {$ins4==1} {
set y [expr ($ins1-1)*20+5]
set i $ins1
set number $ins1
set ins4 0
} elseif {$ins2==1} {
set y $y1
set i $i2
set number $i2
set ins2 0
}
set c $w2.frame.c
global buttonlist bli
global arglist
global b k s n m
global win t$s
label $c.labe$i -text " $number end" -relief raised
$c create window $x1 $y -window $c.labe$i -anchor n -tags item
lappend bli 9
incr number
incr i
incr y 20
}
proc insert {} {
set w .main
set w2 $w.two
set c $w2.frame.c
global i i1 i2 x1 x2 y number del1 ins1 ins2 ins4 y1
global bli buttonlist bli1
#delete the widget after the insert location
for {set number [expr $ins1-1];set i $ins1} {$number<[llength $bli]} {incr number;incr i;incr y -20} {
if {[lindex $bli $number]==1} {
destroy $c.labe$i
destroy $c.entry$i
puts stdout 1
}
if {[lindex $bli $number]==9} {
destroy $c.labe$i
puts stdout 9
}
}
set bli1 {}
for {set i 0} {$i<[llength $bli]} {incr i} {
lappend bli1 [lindex $bli $i]
}
set bli {}
for {set i 0} {$i<$ins1} {incr i} {
lappend bli [lindex $bli1 $i]
}
incr y 20
#create the widget have deleted
for {set num [expr $ins1-1];set number $ins1;set i $ins1} {$num<[llength bli1]} {incr num} {
if {[lindex $bli1 $num]==1} {
forward
puts stdout 1
}
if {[lindex $bli1 $num]==9} {
end
puts stdout 9
}
}
set ins4 1
set ins2 1
set y1 $y
set i2 $i
}

#ins1 the insert location
#ins4 judge the button inserted or not
#ins2 when the button inserted ,the new widget will created after the last button.
THANKS
 
Actually, Bong, if you're using pack, there is a -before option (as well as an -after option) to pack a widget before (or after) an already-packed widget. For example, try this:

[tt]button .b1 -text &quot;First&quot;
button .b2 -text &quot;Second&quot;
button .b3 -text &quot;Third&quot;
pack .b1 -padx 2 -pady 2
pack .b2 -padx 2 -pady 2
pack .b3 -before .b2 -padx 2 -pady 2[/tt]

As for gridded layouts, you can always &quot;regrid&quot; an already gridded widget -- either to change properties like its padding or &quot;stickiness&quot; or to change its location. Here's an example, where I keep regridding an existing label to different cells in a grid by updating its -row and -column grid options (note that this example makes use of some Tk 8.4 features, namely uniform row and column sizes):

Code:
label .l -textvariable loc -font {helvetica 12 bold}   -bg blue -fg white -bd 2 -relief ridge
grid columnconfigure . {0 1 2 3} -weight 1 -uniform A   -minsize 40
grid rowconfigure . {0 1 2 3} -weight 1 -uniform B   -minsize 40

proc move {x y} {
  global loc
  foreach {column row} [grid location . $x $y] {break}
  set loc &quot;$column,$row&quot;
  grid .l -column $column -row $row -sticky nsew
}

bind . <ButtonPress-1> {move %x %y}

move 0 0

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top