0

I am a beginner to Tkinter to python3.8 and I'd like to write a GUI to show the table content as this image.

As you can see, the left lies the function menu while the right displays the table content. Besides, I add scrollbars both vertically and horizontally to the table. I hope the scrollbars could automatically appear to display part of the table correspondingly when I shrink the window like this

However, the scrollbars disappeared after the resizing window and I can't display the rest of my table content.

So I am desperate for how the scrollbars stay by side after shrinking the window.

Here's my code

from tkinter import ttk, StringVar, Tk, VERTICAL, HORIZONTAL, W, NS, N

def read_file():
    global data

    plate_list = ["A", "B", "C", "D", "E"]

    data = [plate_list[0]] + [list(range(i*5, i*5+5)) for i in range(50)]
    return plate_list

def table_refresh(start_index=0, end_index=1):
    ## the name of table
    ttk.Label(right,
              text=f"{data[0]}({start_list.get()} ~ {end_list.get()})                   ",
              font=("System", 30),
              foreground="black",
              padding=20,
              background="#F6F4EC").grid(column=1, row=1, sticky=W)
    ## create the table
    treeview = ttk.Treeview(
        right,
        show="headings",
        columns=["Date", "First", "Second", "Third", "Fourth", "Fifth"], height=15)  # table
    treeview.grid(column=1, row=2)
    vbar = ttk.Scrollbar(right, orient=VERTICAL, command=treeview.yview)
    hbar = ttk.Scrollbar(right, orient=HORIZONTAL, command=treeview.xview)
    ttk.Style().configure("Table.Treeview", background="#F6F4EC", font=("System", 18), rowheight=40)
    treeview.configure(yscrollcommand=vbar.set, xscrollcommand=hbar.set, style="Table.Treeview")
    vbar.grid(row=2, column=2, sticky=NS)
    hbar.grid(row=3, column=1, sticky=NS)


    ttk.Style().configure("Treeview.Heading", font=(None, 20))

    treeview.column("Date", anchor='center') 
    treeview.column("First", anchor='center')
    treeview.column("Second", anchor='center')
    treeview.column("Third", anchor='center')
    treeview.column("Fourth", anchor='center')
    treeview.column("Fifth", anchor='center')
    
    treeview.heading("Date", text="Date") 
    treeview.heading("First", text="First")
    treeview.heading("Second", text="Second")
    treeview.heading("Third", text="THird")
    treeview.heading("Fourth", text="Fourth")
    treeview.heading("Fifth", text="Fifth")
    treeview['show'] = 'headings'
    
    ## add into the table content
    for i, data_row in enumerate(data[(start_index + 1):(end_index + 2)]):
        treeview.insert('', i, values=[date_list[start_index + i]] + data_row)
    # root.update()

def data_refresh():
    global data

    data[0] = plate_list.get()
    read_file()


def plate_refresh(*args):

    data_refresh()

    start_list.current(0)
    end_list.current(0)

    table_refresh()


def submit(*args):
    try:
        start_index = start_list.current()
        end_index = end_list.current() + 1

        table_refresh(start_index, end_index)
    except:
        pass


root = Tk()
root.title("show the table")

root.geometry("%dx%d" % (root.winfo_screenwidth(), root.winfo_screenheight()))


ttk.Style().configure("Main.TFrame", background="#F6F4EC")
mainframe = ttk.Frame(root, style="Main.TFrame", height=root.winfo_screenheight(), width=root.winfo_screenwidth())
mainframe.grid(column=0, row=0, padx=10)

## left:the function menu
left = ttk.Frame(mainframe)
left.grid(column=1, row=1, padx=10, pady=50, sticky=N)
left.rowconfigure(0, weight=1)

ttk.Label(left, text="plate", font=("System", 15, "bold"),
        padding=10).grid(column=1, row=1)
ttk.Label(left, text="start", font=("System", 15, "bold"),
        padding=10).grid(column=1, row=3)
ttk.Label(left, text="end", font=("System", 15, "bold"),
        padding=10).grid(column=1, row=5)

ttk.Style().configure("Bn.TButton", font=('System', '13'), background="blue")
ttk.Button(left, text="提交", command=submit, style="Bn.TButton",
        padding=2).grid(column=1, row=7, sticky=N, pady=20)


ttk.Style().configure("List.TCombobox", font=('System', '50'))
plate = StringVar()
plate_list = ttk.Combobox(left,
                        textvariable=plate,
                        height=20,
                        font=("'@System", 12)) 
plate_list["values"] = read_file() 
plate_list.current(0)  
plate_list.bind("<<ComboboxSelected>>", plate_refresh)
plate_list.grid(column=1, row=2)

date_list = ["Day" + str(i) for i in range(1, 51)]

start = StringVar()
start_list = ttk.Combobox(left,
                        textvariable=start,
                        height=20,
                        font=("'@System", 12)) 
start_list["values"] = date_list[:-1]
start_list.current(0)  
start_list.grid(column=1, row=4)

end = StringVar()
end_list = ttk.Combobox(left, textvariable=end, height=20,
                        font=("'@System", 12))  
end_list["values"] = date_list[1:]
end_list.current(0)  
end_list.grid(column=1, row=6)

## right: where to display table
ttk.Style().configure("Right.TFrame", background="#F6F4EC")
# right = ttk.Frame(mainframe, style="Right.TFrame", width=int(root.winfo_screenwidth()), height=root.winfo_screenheight())
right = ttk.Frame(mainframe, style="Right.TFrame")
right.grid(column=2, row=1, padx=10)
right.rowconfigure(1, weight=1)

table_refresh()


root.mainloop()

Thank you for your time and generosity.

Hao Chen
  • 13
  • 5
  • Did you [read](https://stackoverflow.com/a/3092341/13629335) ? – Thingamabobs Dec 24 '20 at 13:19
  • It would really help if your example didn't depend on external packages such as np and pandas. It's best to use some hard-coded data. – Bryan Oakley Dec 24 '20 at 16:24
  • @Atlas435: that question has nothing to do with this question. – Bryan Oakley Dec 24 '20 at 16:24
  • @BryanOakley May I missunderstand the question, but I think the OP wants to add a scrollbar to a Frame, which isnt possible. Like stated in your answer. – Thingamabobs Dec 24 '20 at 16:35
  • @Atlas435: I think what they are asking is how to keep the scrollbars that they already have from disappearing when they resize the window to a smaller size. – Bryan Oakley Dec 24 '20 at 17:18
  • @BryanOakley That's exactly what I mean. Or just like Excel, there is a horizontal scrollbar at the bottom and a vertical scrollbar on the right side when I resize the Excel window so that I still can move the scrollbars to read all the table content. – Hao Chen Dec 25 '20 at 00:12
  • Please create an example that doesn’t depend on external data or modules. – Bryan Oakley Dec 25 '20 at 01:35
  • @BryanOakley. Sorry for missing your comment. The code given above has already been modified by myself so that the code depends on no external data but ** the simple formed data generated by NumPy and pandas**. Sorry again for this late reply and hope for your help. – Hao Chen Dec 26 '20 at 13:54
  • The fact that it depends on numpy and pandas will prevent a lot of people from being able to run your code. If you want to improve the chance that someone will answer your question, you should remove the dependency on those packages. If your question is about scrollbars disappearing, I can't imagine how the way you compute the data matters at all. – Bryan Oakley Dec 26 '20 at 17:37

0 Answers0