0

I have the main application in file main.py and some UI classes in uiwidgets.py.

in main.py I have:

import uiwidgets as uiw
uiw.MultiColumnListbox(header, data)

def doSomething(self, n)
    do something with n

in uiwidgets.py I have:

class MultiColumnListbox(object):

def __init__(self, header, data):
    self.header=header
    self.data=data

...

    self.tree.bind( "<Double-Button-1>", self.OnClick)

def OnClick(self, event):
    global return_index
    item = self.tree.identify('item',event.x,event.y)
    if item is not "":      
        return_index = (int((item[1:4]),16) - 1)
        n = self.data[return_index][0]

I need to return the n value from class to main.py when the user click the widget. How can I do?

2
  • 1
    from main import doSomething; doSomething(n) Commented Mar 8, 2016 at 10:02
  • in this way I have a circular dependent imports with the error " ImportError: cannot import name 'doSomething' " Commented Mar 8, 2016 at 14:11

1 Answer 1

1

You could just create a global variable in uiwidgets.py at the and and name it 'transfervar' or something like that. Then in main.py you import uiwidgets.py again. It should give you accses to 'transfervar' in main.py.

If the value of n is complicated or long, you can also write it into a textfile. But then you need the know-how how to write and read files. This is very nice to learn in "Think pythonger", by Allen B. Downey, chapter 14.

Your code with the global variable looks like this:

    transfervar = None   #just to create, you could set it 0, too 

    class MultiColumnListbox(object):

    def __init__(self, header, data):
        self.header=header
        self.data=data

    ...

        self.tree.bind( "<Double-Button-1>", self.OnClick)

    def OnClick(self, event):
        global return_index
        item = self.tree.identify('item',event.x,event.y)
        if item is not "":      
            return_index = (int((item[1:4]),16) - 1)
            n = self.data[return_index][0]
            global transfervar     #needs to be declared as global
            transfervar = n
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer but I need to repond to the event, so I need to know if and when the variable transfervar change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.