pythontkinterscrollbartkinter-canvas

Python Tkinter Grid Column width Not expanding need First row not Scorallable


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


Solution

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

    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