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")
def ShowScienceMarks():
    Announcer.say("You got 100 in science")
def ShowSSTMarks():
    Announcer.say("You got 100 in SST")
def ShowEnglishMarks():
    Announcer.say("You got 100 in english")

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

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

if UserInput == "super123":
    Announcer.say("Password is correct")
    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)

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

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.


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:


1 Like

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

1 Like