1

Tengo el siguiente panel emergente de Ayuda que, cuando esté bien construido, irá rellenado de diversos consejos para manejar el programa:

[código]

def ayuda_ayuda(self):
    '''Ventana emergente "Contenido de la Ayuda".'''

    ayuda_top = Toplevel()
    ayuda_top.geometry('420x500')
    ayuda_top.title('Ayuda ~ Editor')
    ayuda_top.config(bg='white', padx=5, pady=5)
    #ayuda_top.resizable(0,0)
    ayuda_top.resizable(1,1)

    ayuda_frame_msgs = Frame(ayuda_top, bg='#767676', height=1000)
    ayuda_frame_msgs.pack(fill='both', expand=1)
    # Texto temporal de prueba
    ayuda_txt = '¡Aloha! ... :)\nFAsdfa fasdf fasdfasdf fasdf fasdfas ffasdfasdf cfasdfasdf fasdf jfghj fgh jFG.'
    #    -> con LABEL
    # El texto NO se ajusta bien a la anchura
    ####ayuda_msg = Label(ayuda_frame_msgs, bg=_cfg__._root_color_quater, ####text=ayuda_txt, width=10, height=29, padx=10, pady=10)
    ####ayuda_msg.pack(ipadx=10, ipady=10)
    #ayuda_msg.config()
    #    -> con MESSAGE
    ayuda_msg = Message(ayuda_frame_msgs, text=ayuda_txt, bg='#242424', padx=10, pady=10)
    ayuda_msg.pack(fill='both', expand=1)
    # Message_width = TopLevel_width - (padx x 2)
    ayuda_msg.config(width=410)
    #print ayuda_msg.config()

    ayuda_frame_btns = Frame(ayuda_top, bg='#242424', padx=10, pady=10)
    ayuda_frame_btns.pack(fill='x', expand=1, side=BOTTOM)
    ayuda_btn_cerrar = Button(ayuda_frame_btns, text='Cerrar', command=ayuda_top.destroy)
    ayuda_btn_cerrar.pack()

y así es tal como se ve al ejecutarlo:

introducir la descripción de la imagen aquí

Y así es como me gustaría que quedará

introducir la descripción de la imagen aquí

La cuestión es que, por mucho que le doy altura al frame_msgs y/o al Message, nunca llega a juntarse o a ocupar el espacio en blanco hasta pegarse con el Frame inferior de los botones ... ¿Por qué? ¿acaso no hay manera de ajustar esto y solo depende del contenido del Message? es decir, ¿únicamente, crecerá en altura dependiendo del contenido del Message? ¿o hay alguna forma de darle una altura fija o por defecto al frame_msgs que no dependa de su contenido y así llegue a pegarse con el frame de los botones?

También, además, que el Message quedara alineado en la parte superior del Frame que lo contiene.

NOTA EXTRA: probé también con un Label en vez de con un Message (aunque el Message si no me equivoco tiene la ventaja de alinear el texto).
Con el Label, no se ajusta el texto a la anchura del widget por eso lo descarté. Y sigo con el Message.

1 respuesta 1

1

Ambos Frames están "juntos" en realidad y se reparten la altura de ayuda_top, no se ven juntos porque el botón tiene un tamaño fijo y no usa todo el espacio de su frame. El problema está en que al segundo frame le dices que se expanda cuando quieres que ocupe en realidad un tamaño fijo, el mínimo para contener sus widgets hijos (ayuda_btn_cerrar).

  • fill hace que el widget llener todo el espacio asignado.

  • expand hace que el administrador pack asigne espacio adicional al widgets si su widget padre tiene espacio de sobra disponible. Si el widget padre se hace más grande de lo necesario para contener todos los widgets contenidos, cualquier espacio excedente se distribuirá entre todos los widgets que tienen establecido fill=True.

Como ambos frames tienen expand=True asignado, el administrador les reparte a partes iguales el espacio sobrante dado que el botón tiene un tamaño fijado. Es decir, simplemente evita que el segundo frame se expanda.

Por otro lado, para que el texto no aparezca centrado sino en la parte superior usa el argumento anchor y sus valores posibles: tkinter.N, tkinter.NE, tkinter.E, tkinter.SE, tkinter.S, tkinter.SW, tkinter.W, tkinter.NW, y tkinter.CENTER. Por defecto es tkinter.CENTER.

# Python 2
import Tkinter as tk
'''
# Python 3
import tkinter as tk
'''

class MyApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.ayuda_ayuda()

    def ayuda_ayuda(self):
        '''Ventana emergente "Contenido de la Ayuda".'''

        ayuda_top = tk.Toplevel(self)
        ayuda_top.geometry('420x500')
        ayuda_top.title('Ayuda ~ Editor')
        ayuda_top.config(bg='white', padx=5, pady=5)
        #ayuda_top.resizable(0,0)
        ayuda_top.resizable(1,1)

        ayuda_frame_msgs = tk.Frame(ayuda_top, bg='#767676')
        ayuda_frame_msgs.pack(fill='both', expand=1)
        # Texto temporal de prueba
        ayuda_txt = '¡Aloha! ... :)\nFAsdfa fasdf fasdfasdf fasdf fasdfas ffasdfasdf cfasdfasdf fasdf jfghj fgh jFG.'
        ayuda_msg = tk.Message(ayuda_frame_msgs,
                               text=ayuda_txt,
                               bg='#242424',
                               foreground="#ffffff",
                               width=420,
                               anchor=tk.N,
                               padx=10, pady=10)
        ayuda_msg.pack(fill='both', expand=1)

        ayuda_frame_btns = tk.Frame(ayuda_top, bg='#000000', padx=10, pady=10)
        ayuda_frame_btns.pack(fill='x', expand=False, side=tk.BOTTOM)
        ayuda_btn_cerrar = tk.Button(ayuda_frame_btns, text='Cerrar', command=ayuda_top.destroy)
        ayuda_btn_cerrar.pack()



if __name__ == "__main__":
    MyApp().mainloop()

introducir la descripción de la imagen aquí

1
  • OK, simples detalles para que funcionara. Gracias.
    – zacktagnan
    el 13 jun. 2018 a las 23:14

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.