Challenge of variable from function



  • I want to add a button in the window. Toplevel, but for that, we need to put her in a variable to indicate where she's going to be.

    But when I do it in the performance of the crypt, the interpreter says the variable. top Not defined. How do I use this variable?

    from tkinter import *
    

    main = Tk()

    main_frame = Frame(main, width="300", height="300").pack()

    def toplevelwin():
    top = Toplevel(main_frame, relief=SUNKEN, width="300", height="300", takefocus=True)

    def close_app():
    main.destroy()

    Button(top, text="Сохранить", relief=SUNKEN).pack()
    Button(main_frame, text="Добавить пароль", command=toplevelwin, relief=FLAT).pack()
    Button(main_frame, width="30", text="Закрыть", command=close_app).pack()

    main.mainloop()



  • Okon Toplevel() does not exist before the challenge toplevelwin() functions. There's no need to create a button until her parent is created:

    def toplevelwin():
        top = Toplevel(main_frame, ...)
        Button(top, text="Сохранить", relief=SUNKEN).pack()
        main.eval('tk::PlaceWindow %s center' % top.winfo_pathname(top.winfo_id()))
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2