Hi, I want to change a variable in a definition everytime it is called.
for example:
when I click on button, it will print 1
when I click on button again, it will print 2
Is this possible?
I have written this code but it doesnt work :)

k=0

def n():
    print k

if n:
    k=k+1
    
from Tkinter import * 
o=Tk()
Button(text="N",command=n).pack(side=LEFT,fill=Y)
mainloop()

(apologise for my english) thank you

Recommended Answers

All 2 Replies

Hi, I want to change a variable in a definition everytime it is called.
for example:
when I click on button, it will print 1
when I click on button again, it will print 2
Is this possible?
I have written this code but it doesnt work :)

k=0

def n():
    print k

if n:
    k=k+1
    
from Tkinter import * 
o=Tk()
Button(text="N",command=n).pack(side=LEFT,fill=Y)
mainloop()

hi

I would suggest that the variable k should be changed in the function

k=0
def n():
    global k
    print k
    k+=1

Annotation:
In my opinion the code is clearer if you do the importing stuff right at the beginning


hi

I would suggest that the variable k should be changed in the function

k=0
def n():
    global k
    print k
    k+=1

Annotation:
In my opinion the code is clearer if you do the importing stuff right at the beginning

Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.