I am a beginner to Tkinter to python3.8 and I'd like to write a GUI to show the table content as .
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
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.