Hi all!
I have a problem with a cycle while and a value list.
How to dynamicize them without knowing what are the values in a list?
And how do I recall a function ( call in my ex.) with the appropriate value?
Thanks
Hud

import tkHyperlinkManager #external lib
from Tkinter import *
i = 0
li_all = [0, 1, 2, 3, 4] #dynamic list

root = Tk()
root.title("Test")

text = Text(root)
text.pack()

hyperlink = tkHyperlinkManager.HyperlinkManager(text)
 
def call(x):
    print x

while i <= 3:
    if i == 0:
        text.insert(INSERT, "this is a ")
        text.insert(INSERT, "link test", hyperlink.add(lambda: call(li_all[0])))        
        text.insert(INSERT, "\n\n")
    if i == 1:
        text.insert(INSERT, "this is a ")
        text.insert(INSERT, "link test", hyperlink.add(lambda: call(li_all[1])))        
        text.insert(INSERT, "\n\n")
    if i == 2:
        text.insert(INSERT, "this is a ")
        text.insert(INSERT, "link test", hyperlink.add(lambda: call(li_all[2])))        
        text.insert(INSERT, "\n\n")
    if i == 3:
        text.insert(INSERT, "this is a ")
        text.insert(INSERT, "link test", hyperlink.add(lambda: call(li_all[3])))        
        text.insert(INSERT, "\n\n")
    print li_all[i]    
    i = i + 1 

mainloop()

Second ex. but the output result remains 4....

import tkHyperlinkManager #external lib
from Tkinter import *

i = 0
li_all = [0, 1, 2, 3, 4] #dynamic list

root = Tk()
root.title("Test")

text = Text(root)
text.pack()

hyperlink = tkHyperlinkManager.HyperlinkManager(text)
   
def call(x):
    print x

while i <= 3:
    text.insert(INSERT, "this is a ")
    text.insert(INSERT, "link test", hyperlink.add(lambda: call(li_all[i])))        
    text.insert(INSERT, "\n\n")
    print li_all[i]    
    i = i + 1 

mainloop()

I don't really understand your description of the problem, but the lambda form looks like it might be wrong. Where you have lambda: call(li_all[i]) are you sure you don't mean lambda [B]i[/B]: call(li_all[i]) ?

ok I try to explain better...

In the first ex.
the li_ all = [0, 1, 2, 3, 4]
the values of the list are called manually
call(li_all[0]
call(li_all[1]
call(li_all[2]
call(li_all[3]

link test when pushed in the text widget the shell output is = 0
link test when pushed in the text widget the shell output is = 1
link test when pushed in the text widget the shell output is = 2
link test when pushed in the text widget the shell output is = 3

this is right.

In the second ex.
li_all = [0, 1, 2, 3, 4]
I want call(li_all)

link test when pushed in the text widget the shell output is = 4
link test when pushed in the text widget the shell output is = 4
link test when pushed in the text widget the shell output is = 4
link test when pushed in the text widget the shell output is = 4
for all link test pushed same output. This is the problem.

And if I have a dynamic list (I do not know how much values I can have)
I do not want to write all lambda manually!

The lambda seems to work....
Any help appreciated
Bye Hud

I too think your lambda is not corrct. Try this:

import tkHyperlinkManager #external lib
from Tkinter import *

i = 0
li_all = [0, 1, 2, 3, 4] #dynamic list

root = Tk()
root.title("Test")

text = Text(root)
text.pack()

hyperlink = tkHyperlinkManager.HyperlinkManager(text)

def call(x):
    print x

while i <= 3:
    text.insert(INSERT, "this is a ")
    q = li_all[i]
    text.insert(INSERT, "link test", hyperlink.add(lambda q: call(q)))
    text.insert(INSERT, "\n\n")
    print li_all[i]
    i = i + 1

mainloop()

Since call() does not return anything, nothing adds to hyperlink.add()

Thanks for response but I have this error when run module and click on hyperlink:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "C:\Documents and Settings\Utente\Desktop\python\tkHyperlinkManager.py", line 36, in _click
self.links[tag]()
TypeError: <lambda>() takes exactly 1 argument (0 given)

Bye Hud

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.