Hello i'm trying to understand urwid library to make my programs:
I have a problem, when i create a button with: urwid.Button('my button', on_press=function()) or like in the code below:
import urwid
def show_or_exit(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
def action():
print("click")
button = urwid.Button(' Press Enter Key ... ')
urwid.connect_signal(button, 'click', action())
button = urwid.Padding(button, align='center', width=20)
button = urwid.Filler(button, 'middle')
mainloop = urwid.MainLoop(button, unhandled_input=show_or_exit)
mainloop.run()
- the function action is called when i launch the program
- when i click the button i have an error : TypeError: 'NoneType' object is not callable
Any idea ?
callbackmeans function's name without()and without arguments.And when you will press button then it will use
()to execute this function.And probably all GUIs need function's name without
()and without arguments.EDIT:
Your code works like
Because
action()returnsNoneso you haveand later it will try to execute
None()And this gives
TypeError: 'NoneType' object is not callableEDIT:
There is other problem in code.
urwidsends button/widget to this function and you have to get this argumentIt sends button and you can assign function to different buttons and inside function you can access pressed button to modify it or get some parameters from button (i.e. get/set text on button).
BTW:
If you will have to use function which gets arguments then you can use
lambdato create function without argumentsAnd if you will have to use
for-loop to create many buttons then you may need to assign value to variable in lambda: