python - Textbox not expanding with containing frame - TKinter -
python - Textbox not expanding with containing frame - TKinter -
i'm building application provides viewports internal info file. files complex, i've created 3 sub-windows handle different aspects of file. upper left provides outline of different sections, upper right provides series of text widgets containing errors found in info file, , lower provides view of datafile itself. facilitate of this, wrote little class serves frame each of these sections , can populated labels, textboxes, etc. (code below.)
the problem i'm running text widgets in upper right , lower sections not expand containing frame. based on various searches of effbot.org, stackoverflow, , others, think have settings correct, wrong. if enlarge main window, each section adapts should, text widgets don't expand left right fill new subwindow dimensions.
any tips appreciated.
here's class provides functionality subwindows:
import tkinter tk class scrollingchildframe(tk.frame): ''' tkinter class creating scrollable window can used in docked multiple document interface form. window created here allows add-on of widgets create scrolling tables (spreadsheet like), groups of text fields, etc. ''' def __init__(self, root): self.count = 0 tk.frame.__init__(self) self.root = root self.canvas = tk.canvas(self, height=self.winfo_height(), width=self.winfo_width() ) self.canvas.grid(row=0, column=0, sticky='nsew') self.vsb = tk.scrollbar(self, orient='vertical', command=self.canvas.yview) self.vsb.grid(row=0,column=1,sticky='ns') self.hsb = tk.scrollbar(self, orient='horizontal', command=self.canvas.xview) self.hsb.grid(row=1,column=0,sticky='ew') self.intframe = tk.frame(self.canvas) self.intframe.config(height=self.winfo_height(), width=self.winfo_width()) self.canvas.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set) self.canvas.create_window(0, 0, window=self.intframe, anchor='nw') #set event bindings self.bind('<configure>', self.onframeconfigure) self.intframe.bind('<configure>', self.onintframeconfigure) def onframeconfigure(self, event=none): ''' adjust canvas when main frame adjusts ''' self.canvas.configure(width=event.width - self.vsb.winfo_width()-2, height=event.height - self.hsb.winfo_height()-2) def onintframeconfigure(self, event=none): ''' adjust scrolling window when internal frame adjusted ''' self.canvas.configure(scrollregion=self.canvas.bbox(tk.all))
here's illustration of how i'm using textboxes don't expand:
import tkinter tk scrollingchildframe import * class vis_gui: ''' main gui class ''' def __init__(self): #tkinter stuff self.root = tk.tk() self.root.geometry('500x500') self.create_frames() self.root.mainloop() def create_frames(self): ''' build gui frames ''' self.root.columnconfigure(0,weight=1) self.root.columnconfigure(1,weight=3) self.root.rowconfigure(0,weight=1) self.root.rowconfigure(1,weight=3) #data blocks self.block_frame = scrollingchildframe(self.root) self.block_frame.config(height=200, width=200) ##error list self.error_frame = scrollingchildframe(self.root) self.error_frame.config(height=200, width=300) ##data self.data_frame = scrollingchildframe(self.root) self.data_frame.config(height=300, width=500) ##populate empty cells self.populateempty() ##place them on grid self.block_frame.grid(row=0, column=0, padx=2, pady=2, sticky='nsew') self.error_frame.grid(row=0,column=1, padx=2, pady=2, sticky='nsew') self.data_frame.grid(row=1, column=0, columnspan=2, padx=2,pady=2, sticky='nsew') def populateempty(self): ''' populate frames empty contents doesn't quite empty. ''' z = tk.text(self.data_frame.intframe) z.insert(tk.insert, 'blah\nblah\nblah') height = float(z.index(tk.end)) z.config( height=height, state=tk.disabled, wrap=tk.none) z.pack(anchor='nw', expand=1, fill=tk.x) z = tk.text(self.error_frame.intframe, height=1) z.pack(anchor='w', expand = 1, fill=tk.x) z = tk.label(self.block_frame.intframe, text = 'no file open') z.pack(anchor='w') if (__name__=="__main__"): wv = vis_gui()
the frame has have expand , fill options set (and have check on scrollingchildframe does-and not complaint incomplete code, pointing out next step). using pack() frame in next code not allow expand. can uncomment , comment other pack if want see difference.
try: import tkinter tk ## python 2.x except importerror: import tkinter tk ## python 3.x top=tk.tk() ## utilize separate frame instead of top fr=tk.frame(top) ##fr.pack() ## not expand fr.pack(anchor='nw', expand=1, fill=tk.x) z = tk.text(fr) insert_text="%s" % ("blah"*25) + 'blah\nblah\nblah' z.insert(tk.insert, insert_text) height = float(z.index(tk.end)) z.config( height=height, state=tk.disabled, wrap=tk.none) z.pack(anchor='nw', expand=1, fill=tk.x) top.mainloop()
python tkinter
Comments
Post a Comment