Daughter window widgets created with Toplevel are displayed in the main window

0

I need help with Python Tkinter 2.7 since I am developing a small graphical interface to manage a database in mysql.

What I want is that by pressing a button, it opens a new window Toplevel that already contains labels and Entrys. I tried with the code that I leave below but when I give the button opens the window well but the Entry and the Label leaves them in the main window and not in the new window. What's wrong?

def win2 ():
  t1 = Toplevel(bg="Orange")
  t1.title("Modificar Datos")
  t1.geometry('600x400')
  t1.focus_set()
  t1.grab_set()
  t1.transient(master=ventana)

  inf=StringVar()
  t1=Entry(ventana,textvariable=inf)
  t1.grid(row=1,column=1)
  Label(t1,text='Hija',bg="red")
    
asked by Erick Finn 10.01.2018 в 23:33
source

1 answer

0

You have two problems mainly:

  • You are rewriting the content of variable t1 . First assign it to the instance of Toplevel but then reuse it with Entry .

  • The widget that belongs to Toplevel must have this as the parent, not the main window as you do in t1=Entry(ventana,textvariable=inf) , this causes the widget to be drawn in the main window as you comment.

That is, it should be something like this:

from Tkinter import * 


ventana = Tk()
ventana.geometry('200x100')

def win2():

    tl = Toplevel(ventana, bg="Orange")
    tl.title("Modificar Datos")
    tl.geometry('600x400')
    tl.focus_set()
    tl.grab_set()
    tl.transient(master=ventana)

    inf = StringVar(tl)
    entry1 = Entry(tl, textvariable=inf)
    entry1.grid(row=0, column=1)
    label1 = Label(tl, text='Hija', bg="red")
    label1.grid(row=0, column=0)


Button(ventana, text="Abrir", command=win2).pack()
ventana.mainloop()
    
answered by 11.01.2018 в 00:28