[Python] Function successfully called from other .py file, but failing to run (Python 3.x,...

Discussão em 'Python' iniciado por Stack, Outubro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I have made a short program that extracts sample accounts data and drops it into a template, controlled by a button that selects which accounting rules to follow.

    Initially, I created two functions(ifrs_selected and us_gaap_selected), and the code worked as expected, i.e. printing either "IFRS" or "US GAAP" to the console and then selecting the relevant template for printing based on the result.

    Original code below:


    ###ENTER ACCOUNTING STANDARDS---------------------------------
    acc_principles = 0 #set the acc_principles variable to 0

    def ifrs_selected(): #create the function to run if IFRS is selected
    global acc_principles
    acc_principles = "IFRS"


    def us_gaap_selected(): #create the function to run if US GAAP is selected
    global acc_principles
    acc_principles = "US GAAP"


    #Create the buttons
    button = tk.Button(standards_enter, width=15, height=4, text="IFRS", command=ifrs_selected) #<--- create IFRS button
    button.pack()
    button = tk.Button(standards_enter, width=15, height=4, text="US GAAP", command=us_gaap_selected) #<--- create US GAAP button
    button.pack()

    standards_enter.mainloop() #<--- set the button selection to run on closing the popup

    print(acc_principles) #<--- print chosen accounting standards to console

    if acc_principles == "IFRS": #<--------- select Word template to use based on accounting standards selected
    template = ifrs_template
    else:
    template = us_gaap_template
    #-------------------------------------------------------------------


    I have been experimenting with calling functions from other files and so decided to move ifrs_selected and us_gaap_selected to another file (accounts_functions.py, in the same folder), and then tried calling them. It appears that they are being pulled into the program correctly, I'm receiving no errors on that point, but when I click the button, I now only get 0 printed to the console, indicating that the function is not running when I click the button. I've been trying to figure out where I'm going wrong here, but I'm at a bit of a loss. Is anyone able to explain where my mistake is, please?

    Code used to pull from other file below:

    ###ENTER ACCOUNTING STANDARDS---------------------------------
    import accounts_functions as af

    acc_principles = 0 #set the acc_principles variable to 0

    #Create the buttons
    button = tk.Button(standards_enter, width=15, height=4, text="IFRS", command=af.ifrs_selected) #<--- create IFRS button
    button.pack()
    button = tk.Button(standards_enter, width=15, height=4, text="US GAAP", command=af.us_gaap_selected) #<--- create US GAAP button
    button.pack()

    standards_enter.mainloop() #<--- set the button selection to run on closing the popup

    print(acc_principles) #<--- print chosen accounting standards to console

    if acc_principles == "IFRS": #<--------- select Word template to use based on accounting standards selected
    template = ifrs_template
    else:
    template = us_gaap_template
    #-------------------------------------------------------------------


    Initial results: either "IFRS" or "US GAAP"

    Results once I change to pull from other file: 0

    Expected results: either "IFRS" or "US GAAP"

    Continue reading...

Compartilhe esta Página