I wanted to try to use tkinter for the first time, and had a little program I wanted to try.
It needed some text in a "table" and because of the number of rows and columns, I needed scrollbars for vertical and horizontal scrolling.
I took two examples from https://pythonguides.com/python-tkinter-scrollbar/ and tried to combine the two to get effectively the first example, with more columns and a horizontal scrollbar.
It works perfectly, except that the horizontal scrollbar doesn't extend across the whole of the bottom of the canvas, but is fixed in the bottom right.
I cannot figure out what I have done wrong, but any guidance would be most appreciated.
#!/usr/bin/python3
import tkinter as tk
root = tk.Tk()
root.title("Customer Data")
# Create a canvas and scrollbar
canvas = tk.Canvas(root)
vertScrollbar = tk.Scrollbar(root, orient=tk.VERTICAL, command=canvas.yview)
horzScrollbar = tk.Scrollbar(root, orient=tk.HORIZONTAL, command=canvas.xview)
# canvas.configure(yscrollcommand=vertScrollbar.set, xscrollcommand=horzScrollbar.set)
#############
canvas.config(
xscrollcommand=horzScrollbar.set,
yscrollcommand=vertScrollbar.set
)
#############
# Create a frame inside the canvas
frame = tk.Frame(canvas)
canvas.create_window((0, 0), window=frame, anchor='nw')
# Add table headers
headers = ['Name', 'Email', 'Phone', 'Location', 'Animal']
for col, header in enumerate(headers):
tk.Label(frame, text=header, font=('Arial', 12, 'bold')).grid(row=0, column=col, padx=10, pady=5)
# Add customer data to the table
customers = [
('John Doe' , '[email protected]', '(123) 456-7890', 'Williamstown, Washington', 'Elephant'),
('Jane Smith', '[email protected]', '(987) 654-3210', 'Williamstown, Washington', 'Elephant'),
('Jane Smith', '[email protected]', '(987) 654-3211', 'Williamstown, Washington', 'Elephant'),
('Jane Smith', '[email protected]', '(987) 654-3212', 'Williamstown, Washington', 'Elephant'),
('Jane Smith', '[email protected]', '(987) 654-3213', 'Williamstown, Washington', 'Elephant'),
# Add more customer data here
]
for row, customer in enumerate(customers, start=1):
for col, item in enumerate(customer):
tk.Label(frame, text=item).grid(row=row, column=col, padx=10, pady=5)
# Update the scroll region
frame.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'))
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
vertScrollbar.pack(side=tk.RIGHT, fill=tk.Y)
horzScrollbar.pack(side=tk.BOTTOM, fill=tk.X)
root.mainloop()
Many thanks for the reply @Seýdylla Gurbangeldiýew. I created a new file and copied your answer verbatim, but it hung. When I managed to exit, I got the error
import-im6.q16: attempt to perform an operation not allowed by the security policy `PS' @ error/constitute.c/IsCoderAuthorized/426.
./fil2.py: line 9: syntax error near unexpected token `('
./fil2.py: line 9: `root = tk.Tk()'
which I didn't understand, so I tried to copy the lines that seemed different to my code:
# Container for canvas and vertical scrollbar
container = tk.Frame(root)
container.pack(fill="both", expand=True)
canvas = tk.Canvas(container)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
vertScrollbar = tk.Scrollbar(container, orient=tk.VERTICAL, command=canvas.yview)
vertScrollbar.pack(side=tk.RIGHT, fill=tk.Y)
horzScrollbar = tk.Scrollbar(root, orient=tk.HORIZONTAL, command=canvas.xview)
horzScrollbar.pack(side=tk.BOTTOM, fill=tk.X)
'''
# Create a canvas and scrollbar
canvas = tk.Canvas(root)
vertScrollbar = tk.Scrollbar(root, orient=tk.VERTICAL, command=canvas.yview)
horzScrollbar = tk.Scrollbar(root, orient=tk.HORIZONTAL, command=canvas.xview)
# canvas.configure(yscrollcommand=vertScrollbar.set, xscrollcommand=horzScrollbar.set)
'''
And that almost worked! What now happens is that initially, the horizontal scrollbar appears, but when I resize the program, so that the horizontal scrollbar isn't needed, it disappears, but doesn't return when needed.
My apologies...
Your help would be most appreciated!
Many thanks @detlef - that provided what I had been looking for without realising it! I will also try changing the packing as suggested as well! Thank you for your help.