Just started learning tkinter for python. I turn from Js frameworks myself. So the question is whether it is possible to assign an ID to a component and call it in the future.

# main.py from gui import * def main(): tk = Tk() tk.geometry("800x450+500+300") tk.resizable(False, False) app = Engine(tk) tk.mainloop() if __name__ == '__main__': main() # gui.py from tkinter import * from tkinter import filedialog, messagebox class Engine(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.initUI() def initUI(self): self.parent.title("Simple") self.place(x=0, y=0, width=800, height=450) btn_open_file = Button(self, # Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΎΠ΅ ΠΎΠΊΠ½ΠΎ text="Open File", # надпись Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ width=20, height=2, # ΡˆΠΈΡ€ΠΈΠ½Π° ΠΈ высота bg="white", fg="black") # Ρ†Π²Π΅Ρ‚ Ρ„ΠΎΠ½Π° ΠΈ надписи btn_open_file.bind("<Button>", self.OpenFile) # ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π›ΠšΠœ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ вызываСтся функция OpenFile text_box = Text() # placing text_box.place(x=20, y=10, height=400, width=600) btn_open_file.place(x=630, y=10) def OpenFile(self, _pass_this_): file_diag = filedialog file_path = file_diag.askopenfilename() # print(file_path) =>>>>>>> text_box.insert(file_path) 

In this case, I want to write the file_path to the text_box widget in the OpenFile function, but I don’t understand how to call text_box in the OpenFile scope

I tried this option.

 btn_open_file = Button(...code..., name="MyTag") catch_id = self.nametowidget(".MyTag") Traceback (most recent call last): File "C:\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Users\dev\PycharmProjects\test\gui.py", line 80, in SaveFile w = self.nametowidget(".da") File "C:\Python\Python37-32\lib\tkinter\__init__.py", line 1353, in nametowidget w = w.children[n] KeyError: 'MyTag' 

1 answer 1

Minimum example:

 from tkinter import * root = Tk() b = Button(root, name='b1') print(repr(b)) # Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ <tkinter.Button object .b1> - Ρ‚.Π΅. имя фактичСски .b1 print(repr(root.nametowidget('.b1'))) # Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ <tkinter.Button object .b1> 

Those. using root.nametowidget('.b1') got the object by its text name.