You already had your event function. Just correct your code to:
"""Create Submit Button"""
self.submitButton = Button(master, command=self.buttonClick, text="Submit")
self.submitButton.grid()
def buttonClick(self):
""" handle button click event and output text from entry area"""
print('hello') # do here whatever you want
This is the same as in @Freak's answer except for the buttonClick() method is now outside the class __init__ method. The advantage is that in this way you can call the action programmatically. This is the conventional way in OOP-coded GUI's.
You already had your event function. Just correct your code to:
"""Create Submit Button"""
self.submitButton = Button(master, command=self.buttonClick, text="Submit")
self.submitButton.grid()
def buttonClick(self):
""" handle button click event and output text from entry area"""
print('hello') # do here whatever you want
This is the same as in @Freak's answer except for the buttonClick() method is now outside the class __init__ method. The advantage is that in this way you can call the action programmatically. This is the conventional way in OOP-coded GUI's.
You should specify a handler, or a function, that is called when you click the Button. You can do this my assigning the name (not calling the function) of the function to the property command of your Button.
For example:
self.submitButton = Button(self.buttonClick, text="Submit", command=buttonClick)
Note the absence of () when assigning buttonClick as the command property of self.submitButton.
Note that you don't need the second parameter called event in your handler/function buttonClick().
How to handle a click button event in python module tkinter - Stack Overflow
Python tkinter button: command calls function without being pressed
calling a python script on button click using python and tkinter
Passing variables into Tkinter button click event
Videos
Starting at the top, let's change your check_answer definition to not create a new label every time:
def check_answer(answer):
if answer == answers[s][0] or answer == answers[s][1]:
global score
score += 1
lbl["text"] = score
Next, we need one small change in your for loop: we want to send answer, not i:
for i in output:
answer = tkinter.Button(window, text=i, command=lambda answer = i: check_answer(answer))
answer.pack()
lbl = tkinter.Label(window, text=score)
lbl.pack()
Lastly, we'll add that label that we removed earlier down to the bottom where you had it initially. You can change the location of this by packing it sooner in the code for aesthetics. Your code still doesn't cycle to a new question once one is answered (correctly or otherwise), but at least now you can see when the user answers correctly.
Hope this helps.
First of all, you did a small error in the lambda callback:
command=lambda answer=i: check_answer(answer)
It should be.
Then for the many labels, create one label and just change the text:
def check_answer(answer):
print(answer)
if answer == answers[s][0] or answer == answers[s][1]:
global score
score += 1
lbl.configure(text=str(score))
(much code i did not copy)
for i in output:
answer = tkinter.Button(window, text=i, command=lambda answer=i: check_answer(answer))
answer.pack()
lbl = tkinter.Label(window, text=score)
lbl.pack()