2

Very similiar to

print("focus object class:", window2.focus_get().__class__) 

taken from here: Python get focused entry name , but I need the exact name of the object. Something like: self.entrywidget_1

OR:

What to fill the place holder to make if true ?

print(self.focus_get().__class__)
if self.focus_get().__class__ == "placeholder":
    print("I work")

The first print returns < class 'tkinter.Entry' >

Community
  • 1
  • 1
Jozef Méry
  • 357
  • 5
  • 15
  • How is the widget supposed to know what name you've assigned it to? You could try looking through e.g. `dir(self)` for the matching object, though. – jonrsharpe Jul 30 '15 at 15:02
  • This sounds like an [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem. Why do you need the name? – Bryan Oakley Jul 30 '15 at 15:09
  • Well I'll exaplin you my app I'm working on. Its a calculator quite complicated. It has a x root y option Im working on and since the x root y is too coplicated for me to implement to the main calculating process I made a check button to create a frame below the main interface with 2 entrys and few buttons to do the calculations. Now I want to be able to type with the interface buttons in these entries, if you can jsut help me sole the problem below the OR I can make it work, by the way mr Oakley this: print("focus object class:", window2.focus_get().__class__) is your comment – Jozef Méry Jul 30 '15 at 15:17
  • I dont know what I have to set to the place holder so that when I press a button on the UI instead of writing to the main Interface it'll write into the entry so if self.focus_get().__class__ == "placeholder": type to entry else: type to main ui – Jozef Méry Jul 30 '15 at 15:19
  • You might think but I have 2 entries ... No I wont have 2 entries only one. The solution is interesing. When u finished typing to the first value for the operatiuon u press a button the entry will be replaced with a label with the text of the value of the entry, then another entry appears, u enter bla bla and do the calculation – Jozef Méry Jul 30 '15 at 15:22

1 Answers1

5

You can't. Any given object can have many names. For example, given the following code, which name would you expect to get?

self.foo = tk.Button(...)
self.bar = self.foo

You rarely, if ever, need the name of the widget. Having a reference to the widget -- like that is returned by focus_get() -- is all you need. With that yo can insert data, delete data, destroy the widget, get the widget contents, etc.

If you really do need some predictable, unique identifier in the case where the same function must work for multiple widgets, you can give each widget a custom attribute that is a symbolic name. You can then get that name at runtime. For example:

e = tk.Entry(...)
e.name = "placeholder"
...
focused_widget = root.focus_get()
print (the name of the focused widget is %s" % focused_widget.name)

Again, you likely won't ever need that. For example, if you want to delete the text in the focused widget, just use the reference to the widget:

focused_widget.delete(0, "end")

If all you really need is to check if the focused widget is a specific widget, just do a direct comparison:

...
self.placeholder = tk.Entry(...)
...
def whatever(self):
    focused_widget = root.focus_get()
    if focused_widget == self.placeholder:
        print("I work")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I need it because In my app anytime the focus is NOT the entry I type to the main UI because my calculator isnt using entry widget as entry but labels which change depending on the UI or keyboard buttons pressed. Thank you its working as excepted !!! – Jozef Méry Jul 30 '15 at 15:40
  • @JozefMéry: if that's the case, just compare the focused widget to your special widget (eg: `if focused_widget == self.placeholder`, if `self.placeholder` is the entry widget you're comparing against) – Bryan Oakley Jul 30 '15 at 15:42
  • Ever been called genius ? no ? well thanks mr. Genius :D I didnt realize that both self.focus_get() and self.widget basically corrspond to the same widget if its in focus – Jozef Méry Jul 30 '15 at 18:01