Livecode not recognizing variable value - livecode

I’ve built an application in Livecode where there are nine buttons on a card. Eight of the buttons play short music samples and have a variable that holds the number of times the button was pushed. Holding down the mouse button allows the buttons to be dragged around the card as well. The ninth button starts a script that surveys the variables associated with the eight sample buttons and ensures that all the samples have been listened to at least once. The script puts the eight variables into one variable and then checks to see if they are empty. If they are, the script returns an error message prompting them to make sure they have listened to all the samples. The problem I’m having is that this works for all but one of the buttons – the one associated with variable gVar08. If this button has not been pressed, gVar08 remains empty and does not trigger the error message. I can’t figure out why. The scripts associated with the sample button and the evaluation button are provided below.
#Code for music sample button
global gVar08
on mouseDown
wait 30
if the mouseClick then
play audioClip "samples/C1/C1-8ConcertoForViolinStringsAndHarpsichordInGR202IAllegroMolto.wav"
set the filename of image "icn08" to "icons/if_abstract_symbol-03_1571964a.png"
add 1 to gVar08
else
grab me
end if
end mouseDown
#Code assigned to 9th button: Check to see if all samples played at least 1x
global gVar01, gVar02, gVar03, gVar04, gVar05, gVar06, gVar07, gVar08, gErr01
local sClct
on mouseUp
put gVar01, gVar02, gVar03, gVar04, gVar05, gVar06, gVar07, gVar08 into sClct
repeat for each item local in sClct
if local = "" then
answer "Have you listened to all of the samples? Be sure to play them all."
add 1 to gErr01
break
end if
end repeat
end mouseDown

You need a comma and “” after gVar08 in the list. When it is empty, the list ends with 7. Adding a this should always include the last value even if empty.

Since local is a reserved word, have you tried using a different variable?

I couldn't get your Music Sample button code to work, probably due to the mouseClick function within a mouseDown handler not triggering, so I came up with this option:
global gVar08
local sMove
on mouseUp
if sMove then
put empty into sMove
exit to top --Avoids playing the audio and adding 1 to gVar08 on move
end if
play audioClip "samples/C1/C1-8ConcertoForViolinStringsAndHarpsichordInGR202IAllegroMolto.wav"
set the filename of image "icn08" to "icons/if_abstract_symbol-03_1571964a.png"
add 1 to gVar08
end mouseUp
on mouseStillDown
if the mouse is down then
put "true" into sMove
grab me
end if
end mouseStillDown
Hope this helps.
Paul

Related

Python 3 / QT Designer - Radio Buttons showing duplicate values in console

I'm using QT designer to try and make a couple of radio buttons with a label that shows which button I've selected in a variable called URL.
I've got the following code so far :
self.radioButton.toggled.connect(self.myradioButton1_function)
self.radioButton_2.toggled.connect(self.myradioButton1_function)
def myradioButton1_function(self):
staging = 'https://staging/URL'
live= 'https://live/URL'
if self.radioButton.isChecked()==True:
URL=staging
if self.radioButton_2.isChecked()==True:
URL=live
self.label.setText("URL is : " +str(URL))
print(URL)
The label display works fine and switches perfectly between live and staging but the problem is with the variable in Python console, when switching between the two buttons - this prints the variable multiple times e.g.
https://staging/URL
https://live/URL
https://live/URL
https://staging/URL
https://staging/URL
https://live/URL
https://live/URL
I want to use the URL variable in another function so need this to store 1 value on selection of a radio button, can you please advise? Many Thanks.
I fixed this by changing the toggled to clicked e.g.
self.radioButton.clicked.connect(self.myradioButton1_function)
self.radioButton_2.clicked.connect(self.myradioButton1_function)

Compiled iOS app automatically opens keyboard on start

I have a program that works correctly in the iOS simulator, but does not work correctly when loaded onto an iOS device (my iPad mini, running iOS 8)
Once the app is launched, the on-screen keyboard automatically opens up. There are no editable fields that are visible on my card. (There is some title text, but the fields are locked.)
I have no commands in preOpenStack, openStack, preOpenCard, etc...
Any advice on how to prevent this?
Apparently, when the card opens, there is a field that gets focus. Perhaps you think a field is locked but it isn't. It can also be a combobox or datagrid that gets focus. Probably, the best way to test this is to run the following script before compiling your app:
on lockFields
repeat with x = 1 to the number of fields
set the lockText of field x to true
end repeat
end lockFields
Run this script once to lock all fields. Now do a test and see if the keyboard shows up. If you have any comboboxes, include them in the script separately, adding something like the following to it:
repeat for each item myItem in "Combobox 1,Combobox 2,Combobox 3"
disable btn myItem
end repeat
Once you know which control is causing the problem, you can add another script to the card:
on enableControls
repeat for each item myItem in "Field A,Field B,Combobox 1,Combobox 2"
enable control myItem
if the name of control myItem contains "field" then
set the lockText of control myItem to false
end if
end repeat
end enableControls
After running this handler, the fields and comboBoxes should be focusable, but the keyboard should not appear.
Important is also that you have no script laying around that creates a native field (which is invisible). If you create such a native field, the keyboard would also appear.

How to set focus to a specific child in a ttk.Treeview similar to clicking on it?

I have an application that has a number of entry widgets and a treeview widget. What I'm trying to do is have a user select a row (child) in the treeview, have certain pieces of data extracted from the child and displayed in differing entry widgets. The user can change or leave whatever data is necessary, hitting 'Return' on each entry widget until the last. Hitting 'Return' on the last entry widget should give focus back to the Treeview, specifically the next child in the list (the row/child immediately below what was originally clicked.
def display_chosen(self, event):#Called when user clicks row/child in 'dirdisplay' Treeview widget
clickedfile = self.dirdisplay.focus()#Get child (row) id of click
self.nextID = self.dirdisplay.next(clickedfile)#Get next child, place in class-wide accessible variable
self.enableentries()#Enable entry boxes so data extraction to entry widgets work
#Do whatever to send data to entry widgets
def enableentries(self):#Sets three entry widgets a, b, c to state normal.
try:
self.a.config(state=NORMAL)
self.b.config(state=NORMAL)
self.c.config(state=NORMAL)
except AttributeError:
pass
def a_to_b(self, event):#While in entry 'a', hitting 'Return' calls this and sets focus to next entry widget, 'b'
#Do whatever with data in a
self.b.focus_set()
def b_to_c(self, event):#While in entry 'b', hitting 'Return' calls this and sets focus to next entry widget, 'c'
#Do whatever with data in b
self.c.focus_set()
def c_to_nex(self, event):#Hitting 'Return' on 'c' calls this, setting focus back to 'dirdisplay' treeview, giving focus to child immediately below what was originally clicked.
#Do whatever with data in c
print('Focus acknowledged')
print(self.nextID)#Feedback to show me the nextID actually exists when I hit 'Return'
self.dirdisplay.focus_set()
self.dirdisplay.focus(self.nextID)
So, this along with the rest of my code (huge, I think I'm showing everything important here, please let me know if more is needed) works in part. a_to_b, b_to_c work correctly. When c_to_nex is called (I know its called when I hit return because of the feedback prints) I know nextID is correct as it does print the correct child ID, but nothing else happens. I know treeview has focus because hitting up or down on the keyboard traverses the rows. I also know that the row nextID describes is 'sort of' in focus because when I hit down, the third row (below the nextID row) is highlighted.
This 'sort of' focus on the nextID row doesnt help me, since I need the row to be selected as if the user clicked on it his or herself.
This
describes a similar question asked a while back, unfortunately, none of those answers helped. I know I'm close, and I know im probably using 'self.dirdisplay.focus(self.nextID)' either incorrectly or with missing options.
Thanks!
After lots of tries, i've finally managed to figure it out!
With self.dirdisplay being a ttk.Treeview widget...
def c_to_nex(self, event):#Hitting 'Return' on 'c' calls this, setting focus back to 'dirdisplay' treeview, giving focus to child immediately below what was originally clicked.
#Do whatever with data in c
print('Focus acknowledged')
print(self.nextID)#Feedback to show me the nextID actually exists when I hit 'Return'
self.dirdisplay.focus_set()
self.dirdisplay.selection_set((self.nextID, self.nextID))
self.dirdisplay.focus(self.nextID)
You need the trio above to act as if you are programmatically setting focus to a specific child/row in your Treeview - as if the user clicked on it his or herself.
self.dirdisplay.focus_set() gives the Treeview widget input focus so that pressing the up/down keys works properly.
self.dirdisplay.selection_set((self.nextID, self.nextID)) gives you the effect of the row in question actually being highlighted. self.nextID is repeated because selection_set seems to want a list of items (as per the documentation) opposed to a single row/child id.
Finally, self.dirdisplay.focus(self.nextID) seems to be what actually gives the row focus (any functions you have bound to <> will activate).

Onkeypress canceling event javascript

I have a textbox that I would like to prevent the user from entering any letters in. They need to be able to enter numbers.
The textbox has an onkeypressed event already set, and I'm adding logic so that if the user enters a letter, nothing is shown.
Regardless of what I do (cancelbubble, stop propogation, return false), the letter is still getting entered in the text box. I've watched the debugger go over these and still the letter is being entered, its like it is taking place after the fact of the event.
The event handler hook you are looking for is onkeydown:
yourInput.onkeydown = function(e){
var char = String.fromCharCode(e.which); // get the char
return /[0-9]/.test(char); //assert it's a number
}
working demo
Returning false from an event handler attached directly (rather than attachEvent) cancels the event.
Actually once I got home to test onkeydown was doing the same thing, when I edited what was in the text box, it would still add the non-numeric character at the end.
Ended up using onkeyup, that appears to be far enough down the line that it will wipe out what is in the textbox.
I was really looking for the ajax filtered textbox functionality, but couldn't use that because we aren't using actual ajax controls in the application.

Difference Between LostFocus Event and Leave Event of TextBox

What is the difference between the LostFocus and the Leave events of TextBox?
Check the notes section on these links:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx
According to MSDN, there is difference when changing focus of a control. The Leave event occurs before a validation and LostFocus occurs after validation.
UPDATE: 14 Feb 2019
I see that I'm still getting views and upvotes on the answer that I posted couple of years ago. It has now become imperative that I include a (rather important) quote from the MSDN links above to avoid confusion among new programmers (note the difference of order esp. in case of focus by using the mouse or by calling the Focus method):
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and
so on), by calling the Select or SelectNextControl methods, or by
setting the ContainerControl.ActiveControl property to the current
form, focus events occur in the following order:
Enter
GotFocus
Leave <--- before validation
Validating --
|<--- validation
Validated --
LostFocus <--- after validation
When you change the focus by using the mouse or by calling the Focus
method, focus events occur in the following order:
Enter
GotFocus
LostFocus <--- before validation
Leave <--- before validation
Validating --
|<--- validation
Validated --
N.B: Emphasis on text and indicators in the quote added by me
They happen at different points in the control's lifecycle. Depending on the method used, validation happens after Leave and before LostFocus.
See MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx
Leave() event means that first executes keyboard event and then executes mouse event where as Lost() event means that first executes mouse event and then executes keyboard event.
To sum up, it either won't work for the keyboard or it won't work for the mouse. Considering you can't predict what input method will be used it's not very helpful.
However, that's not my point. LostFocus will happen when the form loads, Leave does not.
This would turn the text box yellow when the form is loaded.
Private Sub txtBox_LostFocus(sender As Object, e As EventArgs) Handles TextBox.LostFocus
txtBox.BackColor = Color.Yellow
End Sub
This would not.
Private Sub txtBox_TextLeave(sender As Object, e As EventArgs) Handles TextBox.Leave
txtBox.BackColor = Color.Yellow
End Sub

Resources