次のコードは、3回ループして、3つのラベルウィジェットを作成します。
import tkinter as tk
root = tk.Tk()
root.geometry("400x200")
frm = tk.Frame(master=root, bg="yellow")
frm.columnconfigure(index=(0, 1, 2), weight=1)
for i in range(3):
tk.Label(master=frm, text=i, bg="red", padx=40, pady=10).grid(row=0, column=i)
frm.pack(fill="both", expand=True)
root.mainloop()
次の出力が得られます(予想どおり)。
ただし、次のコードは2回ループするだけで、2つのラベルウィジェットを作成します。
import tkinter as tk
root = tk.Tk()
root.geometry("400x200")
frm = tk.Frame(master=root, bg="yellow")
frm.columnconfigure(index=(0, 1, 2), weight=1)
for i in range(2):
tk.Label(master=frm, text=i, bg="red", padx=40, pady=10).grid(row=0, column=i)
frm.pack(fill="both", expand=True)
root.mainloop()
この出力を提供します:
ご覧のとおり、2つのウィジェットの間隔が適切ではありません。私はこの種の出力を取得したかった:
どうすればこれを達成できますか?出力#2のように間隔が不均一なのはなぜですか?
Please keep in mind that I want to do this without creating any other label widgets as I am using a similar method to display search results in an application that I'm making using tkinter. I asked this question because I am facing the same issue in that application when I try to display only two search results or only one search result.
Thanks in advance for answering! And please bare with me as I'm only a beginner at tkinter.
PS: I know this deviates a bit from the question, but if you can point me to some sources/tutorials where I could better learn more about how tkinter works, please do!
Note: I'm using Python v3.8.5
Why is the spacing uneven as in output #2?
The reason is because you are three columns a weight of 1 even though you're only using two columns.
目標が3つの同じサイズの列である場合は、このuniform
オプションを使用する必要があります。weight
未使用のスペースにのみ適用され、同じサイズの列を保証するものではありません。
同じサイズの列を3つ取得uniform
するには、3つの列すべてに同じ値を設定します。すべての列で同じである限り、値が何であるかは関係ありません。
frm.columnconfigure(index=(0, 1, 2), weight=1, uniform="equal")
結果は次のとおりです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加