Reputation: 104
I've got an issue where I've got a scrollbox displaying a few rows each row is about 200 characters wide. I had the width set to 125 which wasn't enough. However when I bump it past about 175 my scroll bars disappear. If I have it at or below 100 it scrolls through all the data but is a very small window. I would like the window to be the size of the frame and scroll through all the code. Code:
import Tkinter as tk
import tkFont
def view():
data = ['|unique_id | id | species | sex_age | collector | location | preparator | collection_date | entered_date | innitials | notes',
'| 88 | A-1444 | puffinus grseus | n/a | n/a | n/a | n/a | 13 May 2013 | 27 Apr 2017 | EB | TL: 395mm WC:',
'| 72 | A-1444 | puffinus grseus | n/a | n/a | n/a | n/a | 13 May 2013 | 27 Apr 2017 | EB | TL: 395mm WC:',
'| 71 | A-1445 | anas clypeata | M | G. Webber | n/a | A. Zack | 23 Oct 2013 | 26 Apr 2017 | EB | TL: 395mm WC:',
'| 87 | A-1445 | anas clypeata | M | G. Webber | n/a | A. Zack | 23 Oct 2013 | 26 Apr 2017 | EB | TL: 395mm WC:']
size = [800, 600]
obj_main_frame = tk.Tk()
data_frame = tk.Frame(obj_main_frame, width=size[0], height=size[1])
scrollbar_y = tk.Scrollbar(obj_main_frame, orient=tk.VERTICAL)
scrollbar_x = tk.Scrollbar(obj_main_frame, orient=tk.HORIZONTAL)
data_scrollable = tk.Listbox(data_frame, font=tkFont.Font(family="Courier", size=10), selectbackground="gray", selectmode=tk.SINGLE, width=150, yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
scrollbar_y.config(command=data_scrollable.yview)
scrollbar_x.config(command=data_scrollable.xview)
obj_main_frame.grid()
data_frame.grid_propagate(0)
data_frame.grid(row=0, column=0, sticky='nsew')
scrollbar_x.grid(row=1, column=0, sticky='ew')
scrollbar_y.grid(row=0, column=1, sticky='ns')
data_scrollable.grid(row=0, column=0, sticky='nsew')
for i, datum in enumerate(data):
data_scrollable.insert(tk.END, datum)
obj_main_frame.mainloop()
return
if __name__ == '__main__':
view()
Upvotes: 0
Views: 731
Reputation: 2643
You made 2 mistakes in your alignment.
First, you should remove the width
argument when defining your listbox, so that it can adjust to the size of its parent:
data_scrollable = tk.Listbox(data_frame,
font=tkFont.Font(family="Courier", size=10),
selectbackground="gray", selectmode=tk.SINGLE,
yscrollcommand=scrollbar_y.set,
xscrollcommand=scrollbar_x.set)
Second, after aligning the listbox in its frame, you must give non-zero weights to the corresponding row and column, to allow the listbox to expand:
data_scrollable.grid(row=0, column=0, sticky='nsew')
data_frame.columnconfigure(0, weight=1)
data_frame.rowconfigure(0, weight=1)
See: Tkinter Grid Manager documentation
Upvotes: 1