python - Tkinter: right align Labels within stretched LabelFrames using grid -
using grid
in tkinter
, i'm trying align set of frames (i love post picture, i'm not allowed.)
i've 2 outer labelframes
of different sizes , on top of each other i'd stretch , align. within bottom frame, i've stack of several other labelframes
, within each of labelframes
there label
. labelframes
extend as outer container , each of inner labels
right align respect containing labelframe
.
i've tried, without success, various combinations of sticky
, anchor
, justify
.
any suggestion, recommendation?
#!/usr/bin/env python import tkinter tk class aligntest(tk.frame): def __init__(self, parent): tk.frame.__init__(self, parent) self.parent = parent self.grid() self.parent.title('align test') self.createmenus() self.createwidgets() def createmenus(self): # menu self.menubar = tk.menu(self.parent) self.parent.config(menu=self.menubar) # menu->file self.filemenu = tk.menu(self.menubar) # menu->quit self.filemenu.add_command(label='quit', command=self.onexit) # create file menu self.menubar.add_cascade(label='file', menu=self.filemenu) def createwidgets(self): # main frame self.mainframe = tk.frame(self.parent) self.mainframe.grid(row=0, column=0) # outer labelframe1 self.outerlabelframe1 = tk.labelframe(self.mainframe, text='outer1') self.outerlabelframe1.grid(row=0, column=0) # inner label self.innerlabel = tk.label(self.outerlabelframe1, text='this longer string, example!') self.innerlabel.grid(row=0, column=0) # outer labelframe2 self.outerlabelframe2 = tk.labelframe(self.mainframe, text='outer2') self.outerlabelframe2.grid(row=1, column=0, sticky='ew') # inner labelframes each single labels self.innerlabel1 = tk.labelframe(self.outerlabelframe2, bg='yellow', text='inner1') self.innerlabel1.grid(row=0, column=0, sticky='ew') self.value1 = tk.label(self.innerlabel1, bg='green', text='12.8543') self.value1.grid(row=0, column=0, sticky='') self.innerlabel2 = tk.labelframe(self.outerlabelframe2, bg='yellow', text='inner2') self.innerlabel2.grid(row=1, column=0, sticky='ew') self.value2 = tk.label(self.innerlabel2, bg='green', text='0.3452') self.value2.grid(row=0, column=0, sticky='') self.innerlabel3 = tk.labelframe(self.outerlabelframe2, bg='yellow', text='inner3') self.innerlabel3.grid(row=2, column=0, sticky='') self.value3 = tk.label(self.innerlabel3, bg='green', text='123.4302') self.value3.grid(row=0, column=0, sticky='') def onexit(self): self.parent.quit() def main(): root = tk.tk() app = aligntest(root) app.mainloop() if __name__ == '__main__': main()
without running code see 2 problems. first aren't using sticky
parameter when calling grid
. part of problem. i've ever used grid
without using parameter.
the second problem aren't giving of rows , columns weight. without positive weight, columns , rows ever use space need contents, , no more. space goes unallocated.
a rule of thumb in every widget being used container other widgets (typically, frames), should give @ least 1 row , 1 column positive weight.
as final suggestion: during development it's helpful give each of frames distinctive color. helps visualize how frames using available space.
Comments
Post a Comment