Resizing listbox with Python Tkinter and grid layout manager -
i trying write simple app 2 side-by-side scrollable listboxes. want them each take half window irrespective of window size. while window resizable, listboxes remain same size , centered horizontally in respective halves. doing wrong?
from tkinter import * import os import sys class scrollablelist(frame): def __init__(self, parent, vscroll=true, hscroll=false): frame.__init__(self, parent) self.grid(sticky=nsew) if vscroll: self.vscrollbar = scrollbar(self, orient=vertical) self.vscrollbar.grid(row=0, column=1, sticky=n+s) if hscroll: self.hscrollbar = scrollbar(self, orient=horizontal) self.hscrollbar.grid(row=1, column=0, sticky=e+w) self.listbox = listbox(self, selectmode=single) self.listbox.grid(row=0, column=0) if vscroll: self.listbox['yscrollcommand'] = self.vscrollbar.set self.vscrollbar['command'] = self.listbox.yview if hscroll: self.listbox['xscrollcommand'] = self.hscrollbar.set self.hscrollbar['command'] = self.listbox.xview self.grid_columnconfigure(0, weight=1) self.grid_columnconfigure(1, weight=0) self.grid_rowconfigure(0, weight=1) self.grid_rowconfigure(0, weight=0) class application(frame): @classmethod def main(cls): nodefaultroot() root = tk() app = cls(root) root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(0, weight=1) root.resizable(true, true) root.mainloop() def __init__(self, parent=none): frame.__init__(self, parent) self.grid(sticky=nsew) options = dict(sticky=nsew, padx=3, pady=4) self.list1 = scrollablelist(self) self.list2 = scrollablelist(self) self.list1.grid(row=0, column=0, **options) self.list2.grid(row=0, column=1, **options) self.grid_rowconfigure(0, weight=1) self.grid_columnconfigure(0, weight=1) self.grid_columnconfigure(1, weight=1) if __name__ == "__main__": application.main()
you forgetting set sticky
attribute when adding actual listbox parent, cause edges of listbox "stick" edges of area allocated them.
self.listbox.grid(..., sticky="nsew")
Comments
Post a Comment