Error in my Tkinter marks program

I wanted the program so that when you press the button then it shows the marks. But in this case it announces it before pressing any button. This problem had occured to me when I wrote my first Button UI code.
This is the code:

import time
import pyttsx3
from tkinter import *
Announcer = pyttsx3.init()



def ShowMathsMarks():
    Announcer.say("You got 100 in maths")
    Announcer.runAndWait()
def ShowScienceMarks():
    Announcer.say("You got 100 in science")
    Announcer.runAndWait()
def ShowSSTMarks():
    Announcer.say("You got 100 in SST")
    Announcer.runAndWait()
def ShowEnglishMarks():
    Announcer.say("You got 100 in english")
    Announcer.runAndWait()


Announcer.say("Hello, Please tell the password to see your marks")
Announcer.runAndWait()

Announcer.say("Enter the secret password to continue")
Announcer.runAndWait()
UserInput = input(": ")

if UserInput == "super123":
    Announcer.say("Password is correct")
    Announcer.runAndWait()
    Announcer.say("Please select which subject's marks you want to see")
    
    Window = Tk()

    MessageLabel = Label(Window, text="Please select the subject's marks")
    MessageLabel.grid(row=1, column=3)

    MathsButton = Button(Window, text="Press to see Unit test 2 maths marks",command=ShowMathsMarks())
    MathsButton.grid(row=4, column=2)

    ScienceButton = Button(Window, text="Press to see Unit test 2 science marks",command=ShowScienceMarks())
    ScienceButton.grid(row=4, column=3)

    SSTButton = Button(Window, text="Press to see Unit test 2 SST marks",command=ShowSSTMarks())
    SSTButton.grid(row=4, column=4)






    mainloop()
else:
    Announcer.say("Wrong password, you don't have access to the marks")
    Announcer.runAndWait()

As you can see you can make a button functional by assigning a function to it. I did this using command=. I have not called the function anywhere else except in the specific button lines

Hello, @PythonBoy. I think, your main problem is about the syntax you use.

[…]command=ShowMathsMarks()[…]

Here, you shouldn’t use the call brackets to let tkinter reserve the function itself. Using them says interpreter to execute the function and pass its “return-value” (None in this case) to argument “command”.
This may be better to use:

[…]command=ShowMathsMarks[…]

1 Like

Ohh ok its done like that, Thanks for your help, this was for my school project. Thanks so much!

1 Like