1

I have three column with scrollbar and it needs to be expand and it should be stretched the window size but i am unable to increase the column width according to the screen size and i need top first row should need to stable that is like heading. the code is below

import tkinter as tk
from tkinter import *
from tkinter import ttk

root = tk.Tk()
root.title("TEST")
root.geometry("800x600")

frame=ttk.Frame(root)
frame.pack(expand=1, fill=BOTH)

canvas = tk.Canvas(frame)
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)

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

RCnt = 0
DataRow = {}
LabCnt = 0;
for gh in range(40):
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"),cursor="hand2", justify=tk.CENTER,relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=0, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"),cursor="hand2", justify=tk.CENTER,relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=1, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"),cursor="hand2", justify=tk.CENTER,relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=2, sticky='ew')
   LabCnt += 1
   RCnt += 1
   frame3s.columnconfigure(gh, weight=1)
   frame3s.rowconfigure(gh, weight=1)

frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

canvas.create_window((0, 0), window=frame3s, anchor="nw")
canvas.grid(row=0, column=0, sticky="nsew")
scrollbar.grid(row=0, column=1, sticky="ns")

def _on_mousewheel(event):
   canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
canvas.bind_all("<MouseWheel>", _on_mousewheel)
while True:
   root.update()

please guide me to achieve expanded column width according to the screen size

4
  • You can set the width of frame3s to the same as the width of canvas inside the callback of event <Configure> on canvas. Commented Dec 2, 2024 at 11:37
  • @acw1668 can you please give me more detail Commented Dec 2, 2024 at 11:40
  • @acw1668 frame3s.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"),width=500,height=500)) is that correct Commented Dec 2, 2024 at 11:58
  • @acw1668 it is not working Commented Dec 2, 2024 at 13:47

1 Answer 1

1

If you want to expand the columns to fit the canvas width, you need to:

  • save the item ID of canvas.create_window((0, 0), window=frame3s, ...)
  • set the width of frame3s to the same as that of canvas inside the callback of event <Configure> on canvas using canvas.itemconfig()
  • set weight=1 on column 0 to 2 using frame3s.columnconfigure()

Below is the updated code:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("TEST")
root.geometry("800x600")

frame = ttk.Frame(root)
frame.pack(expand=1, fill=tk.BOTH)

canvas = tk.Canvas(frame)
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)

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

RCnt = 0
DataRow = {}
LabCnt = 0;
for gh in range(40):
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"), cursor="hand2", justify=tk.CENTER, relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=0, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"), cursor="hand2", justify=tk.CENTER, relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=1, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"), cursor="hand2", justify=tk.CENTER, relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=2, sticky='ew')
   LabCnt += 1
   RCnt += 1

# expand column 0 to 2 to fill the width of the frame
frame3s.columnconfigure((0,1,2), weight=1)

frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

# save the item ID of frame3s
frame_id = canvas.create_window((0, 0), window=frame3s, anchor="nw")

canvas.grid(row=0, column=0, sticky="nsew")
scrollbar.grid(row=0, column=1, sticky="ns")

def _on_mousewheel(event):
   canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")

canvas.bind_all("<MouseWheel>", _on_mousewheel)

# set width of frame3s to the same as that of the canvas
canvas.bind('<Configure>', lambda e: canvas.itemconfig(frame_id, width=e.width))

root.mainloop()  # using .mainloop() instead of while loop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.