Home > other >  How to resize the width of the canvas dynamically
How to resize the width of the canvas dynamically

Time:10-20

I have this code here. Like you see if the text in one of the button is long, it will not all apper. Is there a solution to resize the width of the canvas which is now defined equal to 300 so that it will show all the text in in the buttons. I don't want to do it manually because in the futur the text will be get from an external file and the data can change. Any ideas

from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from time import strftime
import csv
import xmlrpc.client as xmlrpclib

root = Tk()
frame = Frame(root)

container = Frame(root)
canvas = Canvas(container, borderwidth = 0, highlightthickness = 0, width=300, height=500, bg = "white")
#scrollbar = Scrollbar(container, orient="vertical", command=canvas.yview)

vertibar=Scrollbar(
    container,
    orient=VERTICAL
    )
vertibar.pack(side=RIGHT,fill=Y)
vertibar.config(command=canvas.yview)

horibar=Scrollbar(
    container,
    orient=HORIZONTAL
    )
horibar.pack(side=BOTTOM,fill=X)
horibar.config(command=canvas.xview)

scrollable_frame = Frame(canvas)

# setting the minimum size of the root window
root.geometry("300x650")

# Adding widgets to the root window
Label(root, text='my app',
      font=('Verdana', 15)).pack(pady=10)

# tell the canvas how large the frame will be, so that it knows how much it can scroll:

scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)


frame_id =canvas.create_window((0, 0), window=scrollable_frame,  anchor='nw')
canvas.bind("<Configure>",canvas.itemconfig(frame_id, width=canvas.winfo_reqwidth()))

#canvas.configure(yscrollcommand=scrollbar.set)
canvas.config(
    xscrollcommand=horibar.set, 
    yscrollcommand=vertibar.set
    )


    # rechner_full = str(rechner[row])
    # print(rechner_full)
Button(scrollable_frame, text="text1").pack(fill="both", side="top")
Button(scrollable_frame, text="text2").pack(fill="both", side="top")
Button(scrollable_frame, text="text3").pack(fill="both", side="top")
Button(scrollable_frame, text="longtexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt").pack(fill="both", side="top")
    # print(button.cget('text'))


container.pack()
canvas.pack(side="left", fill="both")
#scrollbar.pack(side="right", fill="both")

# Button for closing
exit_button = Button(root, text="Beenden", command=root.destroy)
exit_button.pack(pady=20)


root.mainloop()

CodePudding user response:

Since scrollable_frame will be resized to show the longest button, so you can expand canvas inside the callback bound to <Configure> event on scrollable_frame:

...
scrollable_frame = Frame(canvas)
frame_id =canvas.create_window((0, 0), window=scrollable_frame,  anchor='nw')

def on_frame_resized(event):
    canvas_width = canvas.winfo_reqwidth()
    if event.width > canvas_width:
        # expand canvas to the width of scrollable_frame
        canvas.configure(width=event.width)
    else:
        # expand scrollable_frame to the width of canvas
        canvas.itemconfigure(frame_id, width=canvas_width)
    canvas.configure(scrollregion=canvas.bbox("all"))

# tell the canvas how large the frame will be, so that it knows how much it can scroll:
scrollable_frame.bind("<Configure>", on_frame_resized)
...

However you need to remove root.geometry(...) because it will block the main window from expanding due to resize of canvas. Also you need to remove canvas.bind("<Configure>", ...) because it will mess up the work done by on_frame_resized().

...
#root.geometry("300x650")
...
#canvas.bind("<Configure>",canvas.itemconfig(frame_id, width=canvas.winfo_reqwidth()))
...
  • Related