Search code examples
pythontkintertkinter-layouttkinter-text

When I scroll the table gets stick to the position and only the text gets scrolled. I want it to scroll with the text


When I scroll the table gets stick to the position and only the text gets scrolled. It should scroll with the text. The table was created with entry widget. The code does not throw any error but the scrolling is not working properly.

from tkinter import *
import tkinter as tk
from tkinter import scrolledtext
app = tk.Tk(screenName="main")
#Font and orientation setup
app.geometry('600x200')

txtbox = scrolledtext.ScrolledText(app, width=50, height=10)
txtbox.grid(row=0, column=0,   sticky=E+W+N+S)
txtbox.insert(INSERT,".\n.\n.\n.\n.\n.\n.\n.\n.\n.\n Physical Properties.\n.\n.\n Physical Properties",)
fortable1 = Frame(txtbox, padx=5, pady=5,width=500,height=200 )
fortable1.place(x=0,y=5)
class Table:

    def __init__(self, fortable1):

        # code for creating table
        self.e = Entry(fortable1, width=12, fg='blue', bg="#FFFFE0",
                       font=('Arial', 12, 'bold'), justify=CENTER)

        self.e.grid(row=0, column=0)
        self.e.insert(END, lst[0][0])
        self.e = Entry(fortable1, width=12, bg="#FFFFE0",
                       font=('Arial', 12, 'bold'), justify=CENTER)

        self.e.grid(row=0, column=1)
        self.e.insert(END, lst[0][1])
        self.e = Entry(fortable1, width=12, bg="#FFFFE0",
                       font=('Arial', 12, 'bold'), justify=CENTER)

        self.e.grid(row=0, column=2)
        self.e.insert(END, lst[0][2])
        self.e = Entry(fortable1, width=12, bg="#FFFFE0",
                       font=('Arial', 12, 'bold'), justify=CENTER)

        self.e.grid(row=0, column=3)
        self.e.insert(END, lst[0][3])

        for i in range(1, total_rows):
            for j in range(total_columns):
                self.e = Entry(fortable1, width=12, bg="#FFFFE0",
                               font=('Arial', 12, 'bold'), justify=CENTER)

                self.e.grid(row=i, column=j)
                self.e.insert(END, lst[i][j])


lst = [("Sr/N", 'Parameter', 'Value', "Remarks"),
       (1, 'Grade Category', '1', '-'),
       (2, 'Equivalant Grade', 'N/A', "No such grades \n in our record"),
       (3, 'Liquidus', 'Tliq', '_'),
       (4, 'Solidus', 'Tsol', '_')]

total_rows = len(lst)
total_columns = len(lst[0])
t = Table(fortable1)
app.mainloop()

enter image description here


Solution

  • It is because the frame fortable1 which holds the table is not part of the content of txtbox since it is just put on top of txtbox using .place(). You need to use txtbox.window_create() to insert the frame into the text box instead.

    Below is the updated code:

    ...
    txtbox = scrolledtext.ScrolledText(app, width=50, height=10)
    txtbox.grid(row=0, column=0,   sticky=E+W+N+S)
    fortable1 = Frame(txtbox, padx=5, pady=5,width=500,height=200 )
    #fortable1.place(x=0,y=5)
    # insert the frame using .window_create()
    txtbox.window_create(INSERT, window=fortable1)
    txtbox.insert(INSERT, "\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n Physical Properties.\n.\n.\n Physical Properties")
    ...