Return from function


#1

Hello! I have an issue with getting a value outside function. Im no programmer, jsut an engineer trying to edit a code for my own needs.

The excerpt of my code:

click=0    
# this function is called when our button is clicked
def ButtonFunc(win, data):
    print("Our button was clicked")
    click=1
    return click

# built-in function of the gui
guitk.BCPushButtonCreate(win, "My_Button", ButtonFunc)

print (click)

The print returns "0" istead of the "1" inside the function. Tried
click=ButtonFunc() that I was advised to call it from inside but that issues a complaint that ButtonFunc needs two arguments.

May I ask for a help, please? Tried that elsewhere but got just a pile of arrogance instead.


#2

Which Gui modulo are you using? There are several (pyqt, tkinker, pyside and more)

please post full code, it makes it easier for us, given we can then run the code


#3

OK, thanks. I am using Python 3 interpreter built inside Ansa.

Tried asking on StackOverflow but that guys there seem to jerk their dicks over the fact how great they are and how stupid everybody not knowing something is...

# from the documentation. Creates a box with buttons


import os
import ansa
from ansa import guitk


print("")
print("")
print("")


# this function will be executed when "OK" is clicked in the dialog
# first argument: the name of our dialog box, the second I dont know
def acceptFunc(win, data):
        print("OK clicked")
        return 1					# this is a must. Otherwise the button doesnt do anything


# this is executed upon "cancel"        
def rejectFunc(win, data):
        print("Cancel clicked")
        return 1
 
click=0
def ButtonFunc(win, data):
	print("Our button was clicked")
	click=click+1
	return click


# BCOnExitDestroy is not in the documentation. Probably says that the window is discarded after closing
win = guitk.BCWindowCreate("Moje Okno", guitk.constants.BCOnExitDestroy)


# set window size
guitk.BCWindowSetInitSize(win, 300, 160)


#sets which buttons will be displayed. win=our window object, the other two are minimize and maximize
# if ommited, only close button displays
guitk.BCWindowShowTitleBarButtons(win, guitk.constants.BCMinimizeButton | guitk.constants.BCMaximizeButton)


"""
# only maximize:
guitk.BCWindowShowTitleBarButtons(win, guitk.constants.BCMaximizeButton)


# only minimize:
guitk.BCWindowShowTitleBarButtons(win, guitk.constants.BCMinimizeButton)
"""


# this displays "OK" and "Cancel" buttons. If ommited, these are missing
# if these are not present, the dialog is closed down there with the little red "X"
# notice that this "pejsek" is not called anywhere in the script
pejsek = guitk.BCDialogButtonBoxCreate(win)




guitk.BCWindowSetAcceptFunction(win, acceptFunc, None)
guitk.BCWindowSetRejectFunction(win, rejectFunc, None)


# create a button
# (which window, button_name, function that it does)
guitk.BCPushButtonCreate(win, "My_Button", ButtonFunc)






# guitk.BCPushButtonCreate(win, "Flash", acceptFunc, win)




# guitk.BCWindowSetSaveSettings(win, False)






# this displays the window created
guitk.BCShow(win)


# to call the click function from inside the function
print(click)


print("")
print("")
print("")

#4

This is not a Gui modulo which is easy to find? Without documentation and the ability to run, i can't help. I work with python a lot, including some GUI modules, but each one is slightly different

Yea, if you want to make a question on stackoverflow, you have to do it right


#5

I have come to an engineering solution that works, at least:

clicks=[]		
# this list will contain the number of clicks (as declaring a variable doesnt work
# outside the function for some reason)
def ButtonFunc(win, data):
	clicks.append(1)

...

clicks=len(clicks)
print("The button was clicked",clicks," times.")

This does the job at least...


#6

i should have seen it, its just variable scope.

i think your solution is better, the alternative is to use global:

click=0
def ButtonFunc(win, data):
	print("Our button was clicked")
    global click
	click=click+1
	return click

this will modify the global click variable. Using global is also a bit nasty.

The final solution would be to use partial:

from functools import partial

see docs, then you can do:

def ButtonFunc(win, data, click):
      click = click + 1
      return click
guitk.BCPushButtonCreate(win, "My_Button", partial(ButtonFunc, click))

now buttonfunc should return the correct amount, just make sure you store the returned clicks somewhere, so the click is always the most recent number of clicks


#7

Thanks, I will have a look at that. Although I cannot say that it is understandable at the first glance.


#8

i included partial() to my answer, but without being able to run, i can't fully implement it. partial allows for additional argument when calling the function

just use whatever you see fit :slight_smile:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.