2

I create a tkinter root :

root = tkinter.Tk()

and would like to create a dynamic variable which would be always linked to some property (or is it called a method?) of root :

X = root.winfo_width()

If root's winfo_width() changes, I would like that X is automatically updated.

How to do this? Is it called an alias ? a dynamic variable ?

6
  • 3
    That isn't possible with a plain variable. However, if you have an object obj, you can make it so that obj.x accesses root.winfo_width() under the hood, so it always returns the "synced" value. Commented Dec 13, 2013 at 8:57
  • Ok. How to design such an object? Commented Dec 13, 2013 at 8:58
  • Or another solution : is there a way of creating an alias : X is just a shortcut interpreted as root.winfo_width() ? Commented Dec 13, 2013 at 8:58
  • What do you want to use X for? Commented Dec 13, 2013 at 9:00
  • just read X a lot of times everywhere in my code (not write X, just read it) Commented Dec 13, 2013 at 9:00

4 Answers 4

2

If you just want a shorthand,

X = root.winfo_width

print X()
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks! is it possible to have X (and not X()) as a shorthand for root.winfo_width?
No. X is a value. X() is a function. You want to call a function (root.winfo_width); therefore, you must use a function to do so.
Even if you could write X, it would make your code totally unreadable if X (a value) is suddenly and non-obviously changing.
I'm not talking about the lambdas, but this "X = root.winfo_width()" does not update the value of X if root.winfo_width changes
@Liarez: You typed the wrong thing in your console. Try again without the parentheses.
|
2

The problem is that, although Python is really dynamic, there are a few things that always do exactly the same thing that can't be changed.

One is simple assignment:

x = ...some complex expression...

This always does the same thing: it evaluates "some complex expression", and then x is changed so that it is now a reference to the result of the expression.

The other is variable evaluation:

x

Simply using a single variable never invokes any code or methods or anything -- in the expression where you use x, x is simply "replaced" by whatever it is referring to.

So doing exactly what you want isn't possible, there's no place to add something dynamic. Method lookup is an obvious alternative:

x.y

Here, you could implement x.getattr to return the thing you need (too tricky), or you could give x a property method y (as in the other answers).

The other thing is that x could refer to a mutable object, and it could be x's contents that change. E.g., if x refers to a dictionary,

x = root.winfo_dimensions()
print x['width']

Then you could make it so that root keeps an instance of that dictionary itself, and updates it when the width changes. Maybe it only keeps one and returns that. But then you can also change the contents from elsewhere, and that's tricky.

I wouldn't do any of this at all and just call root.winfo_width() where needed. Explicit is better than implicit.

Comments

2

Depending on what you need exactly, @property decorator may do the trick (based on @BrenBarn's answer):

class X(object):
    @property  # <-- this 'decorator' provides the necessary magic
    def x(self):
        return time.time()  # <-- just for example of refresh
    def __repr__(self):
        return str(self.x)

Now we can do:

>>> x = X()
>>> x
1386929249.63
>>> x
1386929250.27

But that may be a trick linked to the interpreter, but you still can do the following:

>>> x.x
1386929251.14
>>> x.x
1386929253.01

If you have several such "dynamic properties", you can just group them in one class, and access them this way (but this may not be a good idea, in terms of readability, to hide dynamic behavior of a property).

Comments

1

I'll tell you how to make an "alias" of the function, i hope it helps you.

def X():
    return root.winfo_width()

You make that definition, so each time you call "X()" you get the value of root.winfo_width(). If the value of root.winfo_wifth() changes, and you call X(), you get the updated value

Comments

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.