Return from function


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:

# this function is called when our button is clicked
def ButtonFunc(win, data):
    print("Our button was clicked")
    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.


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


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


# 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
def ButtonFunc(win, data):
	print("Our button was clicked")
	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

# to call the click function from inside the function



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


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

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


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

This does the job at least...


i should have seen it, its just variable scope.

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

def ButtonFunc(win, data):
	print("Our button was clicked")
    global click
	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


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


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:


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