anchorを設定することでウィンドウのどの位置に配置するかの指定ができます。
このように使います。
# -*- coding: utf-8 -*- from Tkinter import * root = Tk() root.geometry("300x300") root.title(u"タイトル") button = Button(root, text="CENTER") button.pack(expand=1, anchor=CENTER) root.mainloop()
定数での指定の他に anchor="center" の様に文字列での指定も可能です。
文字列で指定する場合は小文字での記述となります。
# -*- coding: utf-8 -*- from Tkinter import * root = Tk() root.geometry("300x300") root.title(u"タイトル") button = Button(root, text="center") button.pack(expand=1, anchor="center") root.mainloop()
その他に設定可能な値は以下となります。
※位置の指定は方角表されているようです
(北)N=North
(西)W=West
(東)E=East
(南)S=South
位置 | 定数 | 文字列 |
---|---|---|
左上 | NW | nw |
上 | N | n |
右上 | NE | ne |
左 | W | w |
中央 | CENTER | center |
右 | E | e |
左下 | SW | sw |
下 | S | s |
右下 | SE | se |