I am currently learning guizero and I ran into an issue fairly quickly with making a function that disables buttons when clicked. The entire code is a bit much so below is the code that's applicable to the situation.
buttons = ["button0", "button1"]
def disable(n):
buttons[n].disable()
menu = Box(app, layout="grid", grid=[1,0])
button0 = PushButton(menu, command=lambda: disable(0), text="x", grid=[0,0])
button1 = PushButton(menu, command=lambda: disable(1), text="x", grid=[1,0])
Unfortunately, this code returns the following error that is painfully hard to figure out
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/oliver/.conda/envs/py37/lib/python3.7/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "/Users/oliver/.conda/envs/py37/lib/python3.7/site-packages/guizero/PushButton.py", line 197, in _command_callback
self._command()
File "guizero_test.py", line 84, in <lambda>
button0 = PushButton(menu, command=lambda: disable(0), text="0", grid=[0,0])
File "guizero_text.py", line 14, in disable
buttons[n].disable()
AttributeError: 'str' object has no attribute 'disable'
Any help with figuring this out is appreciated!
def disable(n):
buttons[n].disable()
disable() this is the method it calling itself method disable() if assign disable property like that button[n].config(state="disabled")
replace with line 14
button[n].config(state="disabled")
your list just contains strings and you can't perform .config() on a string, try
buttons = [PushButton(menu, command=lambda: disable(0), text="x", grid=[0,0])]
buttons += (PushButton(menu, command=lambda: disable(1), text="x", grid=[1,0]))
when creating your buttons so the list contains the actual button objects
Related
I'm trying to use flash8 to create an drag and drop event using the mouse.
My code is:
import flash.events.MouseEvent;
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN,downf);
circle_mc.addEventListener(MouseEvent.MOUSE_UP,upf);
function downf(e:MouseEvent) { circle_mc.startDrag(); }
function upf(e:MouseEvent) { circle_mc.stopDrag(); }
I get these errors:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement must appear within on/onClipEvent handler
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN,downf);
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Statement must appear within on/onClipEvent handler
circle_mc.addEventListener(MouseEvent.MOUSE_UP,upf);
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: The class or interface 'MouseEvent' could not be loaded.
function downf(e:MouseEvent) {
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: The class or interface 'MouseEvent' could not be loaded.
function upf(e:MouseEvent) { circle_mc.stopDrag(); }
Total ActionScript Errors: 4 Reported Errors: 4
I don't understand why this is happening. On Internet I've found that this error may be caused due to the version of AS3 or AS2, but I also can't find the version I use.
Any help is appreciated.
The code you've posted is clearly AS3 but since you're using Flash 8 you're limited to AS2. No problem though, you can achieve the same using AS2 of course.
Draw a circle using the Oval Tool and select it using the Selection Tool
Hit F8 to convert it to a symbol and hit OK
In the Properties panel give the instance a name e.g. circle_mc
Open the Actions window (F9) and select Scene 1 -> Layer 1 : Frame 1
Paste the following code
circle_mc.onPress = function()
{
startDrag(this, true);
}
circle_mc.onRelease = function()
{
this.stopDrag();
}
I have the following code:
var settingButton:UIButton
settingButton = appDelegate.myFunctionReturningButton()
if (settingButton == nil) {println("WE ARE IN BAD SHAPE!!!!")}
It partly works, but not always. To see what happens in case I do not get what I expect from myFunctionReturningButton(), I added the last line.
But here is the problem and my question:
I get this error message from the Swift compiler:
Could not find an overload for '==' that accepts the supplied arguments
Browsing the net, I kind of understand what that means, but what can I do about it?
How should I write the last line?
For precision:
I have the two following function in AppDelegate.swift and appSettingButton is declared with this line at the top of the AppDelegate class.
var appSettingButton: UIButton = UIButton.alloc()
func registerSettingButton (button:UIButton) {
appSettingButton = button
}
func myFunctionReturningButton() -> UIButton {
return appSettingButton
}
You can't compare a non-optional value to nil because it will never be nil. And you shouldn't use UIButton.alloc() to initialize a button, just use UIButton(). If your logic depends on waiting for this button to be re-defined after initialization of your app delegate subclass, you should make an optional, i.e. UIButton?. Then you can compare it to nil.
#IBAction func rerollTapped(sender: UIButton) {
var pickupLines:[String] = [
"aye babe, you from iraq cause you should babhdad ass up",
"HBB asdjksja asjd aj iueihieu",
"Dollar Menu akjshdskjhdksj",
"askdjkjashkjhd",
"skajshdkasjhdka"
]
var randomPickupLine = arc4random_uniform(UInt32(pickupLines.count))
self.pickupLabel.text = UILabel(named: randomPickupLine)
I'm attempting to randomize pickup lines and display the random line in a label. When the Re-roll button is tapped the next random pickup line is shown in the label.
It seems the problem is with this last line, it says "Extra argument "named" in call"
Any thoughts?
UILabel doesn't have an init method that takes a name (initWithName: in Objective-C) so that's why that is an "extra argument" (to the init() method). Perhaps you were thinking of UIImage?
Also, your randomPickupLine is a number (the index), not the string value.
Also, you are trying to assign label (a UI element) to the text property of another label (which expects a String)
local background = display.newImage("black.png", 0, 0)
local submit = display.newImage("submit.png")
submit.x = display.contentWidth/2
submit.y = display.contentHeight-100
local nameInstructions = display.newText("Enter your name", 10, 50, native.systemFont, 24)
local usersName = native.newTextField(10, 100, 350, 50)
usersName.inputType = "default"
local function keyboardListener (event)
native.setKeyboardFocus(nil)
end
background:addEventListener("tap", keyboardListener)
local function reverseName(event)
reverseUsersName = string.reverse(usersName)
end
submit:addEventListener("tap", reverseName)
local reverse = display.newText(reverseUsersName)
reverse.x = display.contentWidth/2
reverse.y = display.contentHeight/2
Every time that I run this using my Corona SDK thing, I get this:
Bad argument #-1 to 'newText' (string expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'newText'
...Corona Projects/Assignment 4.3/main.lua/src/main.lua:24: in main chunk
reverseName is in the local function reverseName(event), but this function is called once u press or tap on the submit . But here local reverse = display.newText(reverseUsersName), is called before you tap on the sumbit . That is why its giving you error.
The only place that reverseUsersName, accessed on line 24, is set, is inside the function reverseName(event). In this function, reverseUsersName is a global so after the function is run once, that variable will be accessible from other parts of your script, but until then, it does not exist.
Now in line 22 you've registered reverseName as event listener for "tap" events, but events are only generated after your script has been executed once (and in between calls to your script callbacks like reverseName and keyboardListener are callbacks), so when you create the display text just after, the variable does not yet exist.
So what you would have to do is update the text of the reverse display item in your reverseName listener so that every time you click on the button, the reversed name becomes visible. And because of that, you would have to declare your reverse variable above reverseName function so that it is available as an upvalue (read the Corona getting started docs, they are excellent and discuss this subtlety) in that function. And presumably you would want to initialize with showing the usersName rather than reverse.
So you would need something like
local reverse = display.newText(usersName)
local function reverseName(event)
reverseUsersName = string.reverse(usersName)
reverse.SetText(reverseUserName)
end
submit:addEventListener("tap", reverseName)
Note that if you want the string being displayed to be reversed every time you press tap, rather than only first time you press it, you will have to use
local function reverseName(event)
reverseUsersName = string.reverse(reverseUsersName)
reverse.SetText(reverseUserName)
end
reverseUsersName = usersName
Check your function reverseName and the text object(reverse) with the following code:
local reverse --[[ Initialize the object with a global scope,
so you can access it anywhere from the page. --]]
local function reverseName(event)
--[[ In the below line, usersName is a table value. It is the reason
of the error. For getting the string from the text field, you
have to provide 'usersName.text' --]]
reverseUsersName = string.reverse(usersName.text)
reverse.text = reverseUsersName -- Assign the text field value to your text object
end
submit:addEventListener("tap", reverseName)
reverse = display.newText("",20,20,nil,20) --see the parameters of display.newText()*
reverse.x = display.contentWidth/2
reverse.y = display.contentHeight/2
*Corona API: display.newText()
I am trying to extend a LHSprite from LevelHelper so I can add different behavior to different elements.
Ok, so far so good, but what do you want?
Basically imagine a set of characters that have different movements. I want to be able to define a class Character that extends LHSprite and defines a move method. All characters should extend this Character class and define their own movement. This way I can add elements to the map and I can treat them (in terms of movement) in the same way.
Ok, I understood that, but what have you done?
So far I have followed this link about custom LHSprites but I am facing a problem:
The first difference from my case to that one is that I don't add my elements using the LevelHelper (at least the ones I am trying to extend). I add my elements in code because I want a random number of those elements in a random position.
So I have made this init method that creates a cop (that extends Character and Character extends LHSprite). This method actually looks more to "add to loader" method but whatever:
- (id) initInLoader:(LevelHelperLoader *) loader andNumber: (int) i
atPos: (CGPoint) pos{
self = (Cop *) [ loader createBatchSpriteWithName:#"cop_01"
fromSheet:#"copSheet" fromSHFile:#"enemies" ];
_uniqueName = [ NSString stringWithFormat:#"Cop_%d", i + 1 ];
[ self setUniqueName: _uniqueName];
[ self prepareAnimationNamed:#"cop" fromSHScene:#"enemies" ];
[ self setAnimationDelayPerUnit: 1.0f/70.0f ];
self.position = pos;
[ self playAnimation ];
}
So far so good. I can see my cop standing and animated. However, when I try to call the move method (or any other method) it gives me an unrecognized selector crash.
I believe this error happens because of the cast to (Cop *) but I don't know how to surpass this.
I found out that I needed to add a tag to the level helper. However, this tag wasn't updating in the source files so I had to add it manually in the LevelHelperLoader.h, at the enum LevelHelper_TAG.
Then I had to register that tag when I initialize the LevelHelper
[[LHCustomSpriteMgr sharedInstance] registerCustomSpriteClass:[Cop class]
forTag:COP_TAG];
And I had to pass that tag when I get that element from the loader:
self = (GlowWorm *) [ loader createBatchSpriteWithName:#"cop_01"
fromSheet:#"copSheet"
fromSHFile:#"enemies"
tag: COP_TAG];
EDIT
To answer the user that made a new answer with a problem
Don't forget to tell about the new class to LevelHelper before you initialize LevelHelper. You can do that with this line:
[[LHCustomSpriteMgr sharedInstance] registerCustomSpriteClass:[Cop class]
forTag: COP_TAG ];