Create new window in tcl/tk - user-interface

How can i create a same gui every time by clicking the button without closing current one?
wm title . "abcd"
wm geometry . 50x50
pack [button .b -text "new"]
Please help me.

The toplevel command creates a new window for you to put widgets in. It's a good idea to use a procedure for building the overall GUI in that window:
wm title . "abcd"
wm geometry . 50x50
pack [button .b -text "new" -command makeWindow]
set counter 0
proc makeWindow {} {
# Make a unique widget name
global counter
set w .gui[incr counter]
# Make the toplevel
toplevel $w
wm title $w "This is $w"
# Put a GUI in it
pack [label $w.xmpl -text "This is an example"]
pack [button $w.ok -text OK -command [list destroy $w]]
}
Each of these windows that you make is as independent or dependent on the others as you want. It depends on how you write the code, arrange the variables, design the callbacks, etc.

Related

How to get gnuplot label value?

I'm adding labels to a plot. I've followed this tutorial for moving label to best position with the mouse and was glad to discover label can be rewritten at mouse position.
But once it's done, I haven't found any way to get the text value of the label if one wants to move the label at a later time (if in need to adjust position more accurately when zooming say) without copying/pasting the label's text.
In the script, the text is entered and kept around, but I would like to move the label with its id only and get the text or any other way to move the label.
gnuplot> set label 1 "square" at 0,0
> show label
label 1 "square" at (0.00000, 0.00000, 0.00000)
> moveLabel(labelId, text) = sprintf('call "label_loop.gnu" "%s" "%d"', text, labelId)
> eval moveLabel(1, "square") -> should be: eval moveLabel(1)
'label_loop.gnu' for gnuplot-5.0 is the comment part.
Since you mention that you want to call the function just as eval moveLabel(1), I assume that the label has been previously set within your script using set label .... If this is the case, you could modify label_loop.gnu as:
#make sure that label_number is an integer and not a string so that
#it is not "misinterpreted" in "set label"
label_number = int(ARG1);
pause mouse any "adjust label"
#any other button will quit the loop
if( MOUSE_BUTTON == 1 ) {
#using ARG1 instead of label_number or int(ARG1) would
#create a new label with the content of ARG1 as its text
set label label_number at MOUSE_X,MOUSE_Y
print "\n moved label ".ARG1." to position: ",MOUSE_X,MOUSE_Y
replot
call 'label_loop.gnu' ARG1
}
and then use it from the main script for example as:
set term x11
set mouse
set label 1 "square" at 0,0
moveLabel(labelId) = sprintf('call "label_loop.gnu" "%d"', labelId)
plot x
eval moveLabel(1)
The "trick" here is that if you call the set label command without any text, Gnuplot just updates the position and keeps the text as it was...

MEL Querying a selected button

I am new to using MEL to write scripts.
I have two radio buttons, one and two. When radio button 'two' is selected, I want the script to select the two cube objects I have created in my scene (cube1 and cube2), so that when I use my 'rotate' button (a regular push button), both of the cubes rotate.
On the other hand, if the radio button 'one' is selected, then only one of them (cube1) should rotate when I press the rotate button.
I have my radio buttons as follows:
$radio1 = `radioCollection`;
//my radio buttons
$one = `radioButton -label "1 cube"`;
$two = `radioButton -label "2 cubes"`;
radioCollection -edit -select $one $radio1; //so that this one is selected by default
and for the rotate button I have this that rotates the cube object 'cube1' by 30 degrees. This is currently NOT linked to my radio buttons.
button -label "rotate" -command "setAttr cube1.rotateZ `floatSliderGrp -q -value 30.0`";
Thoughts? Should I query the radio button's state? This would be so much easier for me in another language! I could see myself saying something like "if $radiotwo.pressed, then cube1.rotateZ && cube2.rotateZ"
All Maya UI items are completely imperative: you have to issue commands and get results, there is no 'state': the 'button' or whatever will just be the string name of the object you'll use for issuing the commands
To get the state of the radiocollection you call radioCollection -q -select on the collection, which will return the name of the selected radio button; you'd use that to drive your logic.
string $w = `window`;
string $c = `columnLayout`;
string $radiocol = `radioCollection "radio buttons"`;
string $one_cube = `radioButton -label "1 cube"`;
string $two_cube = `radioButton -label "2 cubes"`;
radioCollection -edit -select $one_cube $radiocol;
showWindow $w;
global proc string get_radio_state(string $radio)
{
string $selected = `radioCollection -q -select $radio`;
return `radioButton -q -label $selected`;
}
print `get_radio_state($radiocol)`;
fiddle with the radio buttons and get_radio_state($radiocol); it should return the name of the selected button.
If you're already familiar with other languages, you should probably skip MEL and jump straight to maya python: it's much more capable and less tweaky. Lots of discussion here and here
For comparison, here's a python version of the same idea:
w = cmds.window()
c =cmds.columnLayout()
rc = cmds.radioCollection()
cmds.radioButton('one', label="1 cube")
cmds.radioButton('two', label = "2 cubes")
def print_selected(*ignore):
print "selected", cmds.radioCollection(rc, q=True, select=True)
btn = cmds.button("print selected", command=print_selected)
cmds.showWindow(w)
Here the button does the same thing as the print statement in the earlier example

Populate text field with variable and show text

Here's my code:
set todayDate to do shell script "date '+%Y/%m/%d'"
set salesRef to "COMPANY-AB" & "-" & theCustomerInitials & "-" & todayDate & "-" & theCustomerID
set the clipboard to salesRef as text
display dialog salesRef as text buttons {"OK"} default button 1
Here's the UI: http://i.imgur.com/sfRnpr6.png
I'd like to make the text 'Copied!' appear when the successfully completes the clipboard line, but remain hidden until then, and also populate the text field at the bottom with the 'salesRef' variable, so the user get's an output.
Essentially then, I'll be able to remove the display dialog. But I can't work out how to do this :(
Successfully managed this by adding these lines:
property theTextField : missing value
…
theTextField's setStringValue:salesRef
Then created a connection to the text field in IB by control dragging from the App Delegate to the text field, and selecting the property.
Are you ok with NSAppleScript?
if so here is an example:
NSAppleScript *myScriptThatShouldGimmeStuff = [[NSAppleScript alloc] initWithSource:
#"return (text returned of (display dialog \"gimme your input:\" default answer \"\" buttons {\"Ok\"} default button 1))"];
//this previous line is the applescript
NSAppleEventDescriptor *theStuff = [myScriptThatShouldGimmeStuff executeAndReturnError:nil];
self.myTextBoxToPutInputIn.stringValue = [theStuff stringValue]; //[theStuff stringValue] is the result returned
/|\
/ | \
/ | \
|
|
//don't forget to change this
//(I didn't comment out the arrow because i want xcode
// to throw you an error so you don't forget to change that).
So it is not clear from you code what you are doing, but IF you are using ASOC, then you have to define OUTLETS to the controls in your UI, and then you just set the .stringValue of the outlet to whatever string you want to appear in them.
If you don't know how to set up outlets, watch this video and download the example code (look for INTRO VIDEO on the page):
http://www.macosxautomation.com/applescript/develop/index.html
Or for another step-by-step example, here:
http://asobjcresources.weebly.com/getting-a-text-field-input.html

Dropdown menu button in tcl/tk

Is there any way to create a drop down toolbar button(Like Paste button in MS Word) using Tcl/TK?.I have googled a lot but nothing found.Any help will be appreciable.
You probably want to use a menubutton and a menu, probably with radiobutton entries if I've understood which UI element in Word you're talking about. (Or maybe a ttk::menubutton and menu.) Now, if you're doing something very simple with it then you can make do with just tk_optionMenu as a way to combine those commands, but that's just a simple procedure; if you're doing something complicated with the menu, it's probably easier to write it yourself or at least to get the code for tk_optionMenu and to customise it how you want it to work.
The source code for tk_optionMenu isn't very long; I'll paste the non-comment parts of it here:
proc ::tk_optionMenu {w varName firstValue args} {
upvar #0 $varName var
if {![info exists var]} {
set var $firstValue
}
menubutton $w -textvariable $varName -indicatoron 1 -menu $w.menu \
-relief raised -highlightthickness 1 -anchor c \
-direction flush
menu $w.menu -tearoff 0
$w.menu add radiobutton -label $firstValue -variable $varName
foreach i $args {
$w.menu add radiobutton -label $i -variable $varName
}
return $w.menu
}
You probably want to pay attention to how the menubutton and menu are connected to each other. ttk::menubutton is mostly a drop-in replacement for menubutton, except for different look-and-feel configuration options.

How to implement tk scrollbar for multiple listboxes (TCL)?

I tried all sort of options but no success in implement simple scrollbar for two or more listboxes. Following is my code giving error while scrolling. I hope you guys are helping me...
scrollbar .scroll -orient v
pack .scroll -side left -fill y
listbox .lis1
pack .lis1 -side left
listbox .lis2
pack .lis2 -side left
for {set x 0} {$x < 100} {incr x} {
.lis1 insert end $x
.lis2 insert end $x
}
.lis1 configure -yscrollcommand [list .scroll set]
.lis2 configure -yscrollcommand [list .scroll set]
.scroll configure -command ".lis1 yview .lis2 yview ";
thanking you.
There are a number of examples on the Tcler's wiki, but the core principle is to use a procedure to ensure that the scrolling protocol is synchronized between the widgets. Here's an example based off that wiki page:
# Some data to scroll through
set ::items [lrepeat 10 {*}"The quick brown fox jumps over the lazy dog."]
# Some widgets that will scroll together
listbox .list1 -listvar ::items -yscrollcommand {setScroll .scroll}
listbox .list2 -listvar ::items -yscrollcommand {setScroll .scroll}
scrollbar .scroll -orient vertical -command {synchScroll {.list1 .list2} yview}
# The connectors
proc setScroll {s args} {
$s set {*}$args
{*}[$s cget -command] moveto [lindex [$s get] 0]
}
proc synchScroll {widgets args} {
foreach w $widgets {$w {*}$args}
}
# Put the GUI together
pack .list1 .scroll .list2 -side left -fill y
It's worth noting that you can also plug any other scrollable widget into this scheme; everything in Tk scrolls the same way (except with -xscrollcommand and xview for horizontal scrolling, together with a change of scrollbar orientation). Furthermore, the connectors here, unlike the ones on the wiki page, can be used with multiple groups of scrolled widgets at once; the knowledge of what is scrolled together is stored in the -command option of the scrollbar (first argument to synchScroll callback).
[EDIT]: For 8.4 and before, you need slightly different connector procedures:
# The connectors
proc setScroll {s args} {
eval [list $s set] $args
eval [$s cget -command] [list moveto [lindex [$s get] 0]]
}
proc synchScroll {widgets args} {
foreach w $widgets {eval [list $w] $args}
}
Everything else will be the same.
I know that this post is really old but I recently discovered what I think is a fairly simple solution. Instead of using the vertical scrollbar I found that I can use the slider widget. With the slider you can get the position of the slider and use that to set the yview of the listbox(es). Multiple listboxes can be scrolled simultaneously. I use vtcl to build the GUI so the code I can provide may not beimmediately obvious for those using tk wm comands. But here is the code I use. It is bound to slider motion.
set listIndex [$widget(Scale1) get]
$widget(Listbox1) yview $listIndex
$widget(Listbox2) yview $listIndex
Hope that is helpful to somebody.
If you plan to do much work in the the callback command - make a procedure to do it as that is both faster (the procedure gets byte compiled) and less likely to introduce tcl syntax problems. In this case you are trying to perform two tcl commands in the scrollbar function so you need to separate the statements using newline or a semicolon.
Calling the scrollbar set function from both listboxes will just have the second one override the first. You either need a function to merge those two or if the lists have the same lengths, just call it from one of them to set the scrollbar size and position and then update all listboxes with the scrollbar callback.
There is a multilistbox package somewhere - try the Tcl wiki to locate examples.

Resources