LoginSignup
0
1

More than 3 years have passed since last update.

残プロ 第-16回 ~tkinterでウィジェットを均等配置~

Last updated at Posted at 2021-06-17

stickyを指定しても均等に配置されない

tkinterでは起こりがちな問題なんですが,記事が見当たらなかったのでここに書いておきます.


まず,下の図をご覧ください.

inframe.png

tkinterのウィジェットはpack,grid,placeで配置できます.その際にstickyを指定やることで引き延ばし方向が指定できるのですが,これ例だと上手くいってませんね...

import tkinter as tk

root = tk.Tk()

frame = tk.LabelFrame(root, text="label frame")
frame.grid(row=0, column=0)

btn = tk.Button(frame, text="button")
btn.grid(row=0, column=0, sticky=tk.NSEW)

label = tk.Label(frame, text="label")
label.grid(row=1, column=0, sticky=tk.NSEW)

root.mainloop()

.grid_rowconfigureを使う

.grid_rowconfigureと.grid_columnconfigureを指定することで解決します.
指定は行数・列数分行う必要があります.

frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1