In the following program, adaptation of an example from the forum , I would like that when it is executed, it would initially be presented with enough horizontal size to see the complete label, and not have to scroll horizontally.
I read that all widgets adapt, in size, to contain their dependent widgets, but here I can't find a reason why it shouldn't.
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()
That is, it looks like this:
when I would like the initial presentation to be this:
Where should I touch or what am I missing?
Note: the colors are forced to be able to distinguish each frame. I am learning :-)
The Canvas widget does not change its size to fit its content, if you want it to start with the minimum size to contain the item, you can change its width using the information provided by
bbox(tk.ALL)
, once the items are added in the initializer:The complete code: