En el siguiente programa, adaptación de un ejemplo del foro, me gustaria que cuando se ejecuta se presentase, inicialmente, con el tamaño horizontal suficiente para ver la etiqueta completa, y no tener que hacer scroll horizontal.
Leo que todos los widgets de adaptan, en tamaño, para contener a sus widgets dependientes, pero aquí no encuentro la razón de que no se haga así.
import tkinter as tk
from tkinter import font as font
muestra_low = "abcdefghijklmnñopqrstuvwxyz"
muestra_up = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"
class MyApp(tk.Frame):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.configure(bg="Light Blue", bd=3, relief=tk.RIDGE)
self.grid(sticky=tk.NSEW)
# Create a frame for the self.canvas and scrollbar(s).
frame2 = tk.Frame(self)
frame2.grid(row=0, column=0, sticky=tk.NSEW)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# Add a self.canvas in that frame.
self.canvas = tk.Canvas(frame2, bg="Yellow")
self.canvas.grid(row=0, column=0, sticky=tk.NSEW)
frame2.columnconfigure(0, weight=1) # !!!!!!!!!!!!!!! Permite crecer self.canvas horizontalmente
frame2.rowconfigure(0, weight=1)
# Create a vertical scrollbar linked to the self.canvas.
vsbar = tk.Scrollbar(frame2, orient=tk.VERTICAL, command=self.canvas.yview)
vsbar.grid(row=0, column=1, sticky=tk.NS)
self.canvas.configure(yscrollcommand=vsbar.set)
# Create a horizontal scrollbar linked to the self.canvas.
hsbar = tk.Scrollbar(frame2, orient=tk.HORIZONTAL, command=self.canvas.xview)
hsbar.grid(row=1, column=0, sticky=tk.EW)
self.canvas.configure(xscrollcommand=hsbar.set)
# Create a frame on the self.canvas to contain the labels.
labels_frame = tk.Frame(self.canvas, bg="Red", bd=2)
###############################
self.portLabela = tk.Label(labels_frame, text= "Courrier", font="Courrier")
self.portLabela.grid(column=0, row=0, sticky="w")
self.portLabelb = tk.Label(labels_frame, text= muestra_low, font="Courrier")
self.portLabelb.grid(column=1, row=0, sticky="w")
self.portLabelb = tk.Label(labels_frame, text= muestra_up, font="Courrier")
self.portLabelb.grid(column=2, row=0, sticky="w")
###############################
# Create self.canvas window to hold the labels_frame.
self.canvas.create_window((0,0), window=labels_frame, anchor=tk.NW)
labels_frame.update_idletasks() # Needed to make bbox info available.
bbox = self.canvas.bbox(tk.ALL) # Get bounding box of self.canvas with Labels.
self.canvas.configure(scrollregion=bbox)
if __name__ == "__main__":
root = tk.Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
app = MyApp(root)
app.mainloop()
Es decir, se presenta asi:
cuando me gustaria que la presentación inicial fuese esta:
¿Donde debo de tocar o que estoy omitiendo?
Nota: los colores están forzados para ser capaz de distinguir cada frame. Estoy aprendiendo :-)