简体   繁体   中英

How to call a function in python at different times?

I need to call a certain function when the button is clicked and the code I wrote works perfectly only when the button is pressed the first time. If I press the button again It won't call the function but it prints the string I coded to print when the button is pressed.What can I use to make the function able to call multiple times? Thanks. Here is the code. Please Help Me

sg.theme('DarkAmber')
layout = [  [sg.Text('URL'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')]]
window = sg.Window('Window Title', layout)
while True:
    event, values = window.read()
    if event in (None, 'Cancel'):   # if user closes window or clicks cancel
        break
    url = values[0]
    Mainfunction()
    print('Button pressed here is the value ', values[0])

I actually made an simple error that would make the function work the correct way on the second time it is called. So Myfunction is like

var1 = 0
def Function:
    if(var1 == 0):
        //do some shit
        var1 = var1 + 1
    if(var1 == 1):
        //do some shit
        var1 = var1 + 1   

So, I found out the Function is being called and executed but the arguments does not meet their requirements(Or some wierd shit. Am a NOOB) defining the variable inside the function made would do the work.Likeways, The code would be,

def Function:
    global var1 // If you wanna make it global
    var1 = 0
    if(var1 == 0):
        //do some shit
        var1 = var1 + 1
    if(var1 == 1):
        //do some shit
        var1 = var1 + 1  
``` Hope it Helped You

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM