[Python] How to line left justify label and entry boxes in Tkinter grid

在 tkinter 裡 grid 區塊如何對齊的方式。直接調整 .grid() 裡的屬性 sticky = W, sticky = E, sticky = N。我試了發現 :

  • W = LEFT,
  • E = RIGHT
  • N = Center, default 值是 N.

Normally to accomplish what you are after you would simply put sticky = W in the grid options for the Entry widget to left-justify the contents of the cell. However, this will only work for each individual Frame, leaving the contents of each separate LabeledFrame out of alignment.

Easiest way to fix this without changing much code:

You’ll want to add a line to your for loop. If you specify a large minimum-width of the column that self.field’s Frame is inserted into, you can be sure that things will align how you want them to. I’ve also added config options to the grid calls within the LabeledEntry class: sticky = W for the Label & sticky = E for the Entry.


使用 .grid() 可以把元件加入到 layout 裡, 使用 .grid_forget() 可以 hide。

Tkinter: how to show and hide Frame class:

f will be a class, not an instance. Since self.frames is a dictionary with the actual frames being the values, you want to loop over the values:

for f in self.frames.values():
    f.grid_forget()

在套用 UI 時,還有一些參數可以使用,例如:padx、pady

config(**options)
Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.
**options
Widget options.
background=
The background color to use in this frame. This defaults to the application background color. To prevent updates, set the color to an empty string. (the option database name is background, the class is Background)
bg=
Same as background.
borderwidth=
Border width. Defaults to 0 (no border). (borderWidth/BorderWidth)
bd=
Same as borderwidth.
class=
Default is Frame. (class/Class)
colormap=
Some displays support only 256 colors (some use even less). Such displays usually provide a color map to specify which 256 colors to use. This option allows you to specify which color map to use for this frame, and its child widgets. 
By default, a new frame uses the same color map as its parent. Using this option, you can reuse the color map of another window instead (this window must be on the same screen and have the same visual characteristics). You can also use the value “new” to allocate a new color map for this frame. 
You cannot change this option once you’ve created the frame. (colormap/Colormap)
container=
Default is 0. (container/Container)
cursor=
The cursor to show when the mouse pointer is placed over this widget. Default is a system specific arrow cursor. (cursor/Cursor)
height=
Default is 0. (height/Height)
highlightbackground=
Default is system specific. (highlightBackground/HighlightBackground)
highlightcolor=
Default value is system specific. (highlightColor/HighlightColor)
highlightthickness=
Default is 0. (highlightThickness/HighlightThickness)
padx=
Horizontal padding. Default is 0. (padX/Pad)
pady=
Vertical padding. Default is 0. (padY/Pad)
relief=
Border decoration. The default is FLAT. Other possible values are SUNKENRAISEDGROOVE, and RIDGE
Note that to show the border, you need to change theborderwidth from its default value of 0. (relief/Relief)
takefocus=
If true, the user can use the Tab key to move to this widget. The default value is 0. (takeFocus/TakeFocus)
visual=
No default value. (visual/Visual)
width=
Default value is 0. (width/Width)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *