Tkinter buttons not performing command

Question:

I am trying to create a counter for Jeopardy so that my Mom and I can keep score. Currently the program that I am creating is assigning the values to variables and then not performing the command when I press the button in the window. I am running Python 2.7.13.`
import Tkinter as tk

root = tk.Tk()
root.title("Jeopardy Scores")

def ChangeScore(User,Value):
    if User == 1:
         Score = int(JScore.get())
         JScore.set(Score + Value)
         #J = JScore.get()
         #print J
         #SayHi()

    else:
         Score = int(MScore.get())
         MScore.set(Score + Value)
         #M = MScore.get()
         #print M
         #SayHi()

#def SayHi(*args):
    #print 'hi'

MainFrame = tk.Frame(root)
MainFrame.grid(column=0, row=0)
MainFrame.columnconfigure(0, weight=1)
MainFrame.rowconfigure(0, weight=1)

JScore = tk.StringVar()
MScore = tk.StringVar()
JScore.set(0)
MScore.set(0)


JL = tk.Label(MainFrame, text = "Joey's Score", padx = 10, pady = 2)
JL.config(bg = 'blue', fg = 'yellow', font = ('Arial',30, 'bold'))
JL.grid(column = 0, row = 0)
ML = tk.Label(MainFrame, text = "Mom's Score", padx = 10, pady = 2)
ML.config(bg = 'blue', fg = 'yellow', font = ('Arial',30, 'bold'))
ML.grid(column = 1, row = 0)



JSS = tk.Label(MainFrame, textvariable=JScore ,padx = 122)
JSS.config(bg = 'blue', fg = 'yellow', font = ('Arial',30, 'bold'))
JSS.grid(column = 0, row = 1)

MSS = tk.Label(MainFrame, textvariable = MScore,padx = 122)
MSS.config(bg = 'blue', fg = 'yellow', font = ('Arial',30, 'bold'))
MSS.grid(column = 1, row = 1)


for i in range(1,6):
    Score = tk.IntVar()
    Score.set(i*200)
    Score1 = 200*i
    JButton = tk.Button(MainFrame, textvariable = Score, command = 
                        ChangeScore(1,Score1))
    JButton.grid(column = 0, row = 1+i)
    MButton = tk.Button(MainFrame, textvariable = Score, command = 
                        ChangeScore(2,Score1))
    MButton.grid(column = 1, row = 1+i)

JButton = tk.Button(MainFrame, text = '400', command = ChangeScore(1,400))
JButton.grid(column = 0, row = 7)


root.mainloop()

The code runs and produces this Window

Note that no buttons have been pressed when the picture was taken. It appears that all the buttons are ‘being pressed’ when the code runs and then nothing happens when i press the buttons afterwards.

I have no experience with Tkinter beyond the small information that has allowed me to do this and I have a bit more experience with Python. I am mainly doing this as an excerise for myself to improve my coding and to acutally use for Jeopardy!. Any help would be appreciate

Asked By: Joey Macaluso

||

Answers:

Here the command parameter for Button should be a callable. You should not call the function yourself and pass the return value to it. Instead, you provide a function that is to be called later.

So change you code to things like

command=lambda: ChangeScore(1, 400)

to create a lambda to be called later will solve the problem.

Answered By: doraemon
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.