Я, по жизни, не могу понять, как использовать grid()
для управления виджетами внутри Frame
( Python 3.6 ).Код ниже пытается показать матрицу 2 × 2 ListBox
es в корневом окне.
import tkinter as TK
root = TK.Tk()
root.title('My App')
rootWidth = 768
rootHeight = 768
root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
root.resizable(width=False, height=False)
frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box00 = TK.Listbox(frame00, bd=0)
box10 = TK.Listbox(frame00, bd=0)
box00.grid(row=0, sticky=TK.N)
box10.grid(row=1, sticky=TK.S)
frame00.grid(row=0, column=0, sticky=TK.W)
frame00.rowconfigure(0, weight=1)
frame00.rowconfigure(1, weight=1)
frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box01 = TK.Listbox(frame01, bd=0)
box11 = TK.Listbox(frame01, bd=0)
box01.grid(row=0, sticky=TK.N)
box11.grid(row=1, sticky=TK.S)
frame01.grid(row=0, column=1, sticky=TK.E)
frame01.rowconfigure(0, weight=1)
frame01.rowconfigure(1, weight=2)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
for i in range(20):
box00.insert(TK.END, 'test')
box10.insert(TK.END, 'test')
box01.insert(TK.END, 'test')
box11.insert(TK.END, 'test')
В конце я вижу только две ListBoxes
(то есть только одну строку)вместо 4 из них в моем графическом интерфейсе.Но, если я использую один Frame
на ListBox
, то все работает.
import tkinter as TK
root = TK.Tk()
root.title('My App')
rootWidth = 768
rootHeight = 768
root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
root.resizable(width=False, height=False)
frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box00 = TK.Listbox(frame00, bd=0)
frame10 = TK.Frame(root, bd=2, relief=TK.RAISED)
box10 = TK.Listbox(frame10, bd=0)
box00.grid(row=0, sticky=TK.N)
box10.grid(row=0, sticky=TK.S)
frame00.grid(row=0, column=0, sticky=TK.W)
frame10.grid(row=1, column=0, sticky=TK.W)
frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box01 = TK.Listbox(frame01, bd=0)
frame11 = TK.Frame(root, bd=2, relief=TK.RAISED)
box11 = TK.Listbox(frame11, bd=0)
box01.grid(row=0, sticky=TK.N)
box11.grid(row=0, sticky=TK.S)
frame01.grid(row=0, column=1, sticky=TK.E)
frame11.grid(row=1, column=1, sticky=TK.E)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
for i in range(20):
box00.insert(TK.END, 'test')
box10.insert(TK.END, 'test')
box01.insert(TK.END, 'test')
box11.insert(TK.END, 'test')
Это то, что внутри Frame вы можете использовать только pack()
?
ОБНОВЛЕНИЕ
Люди в ветке указали, что в исходном списке кодов я не использовал аргумент ключевого слова column
в этих вызовах grid()
.Фактически, я сделал, просто я удалил их в своей последней попытке перед публикацией, которая в основном привела к тому же самому.
Вот новая версия, которая включает в себя column
аргументы, которые показывают только два ListBox
ес тоже.
root = TK.Tk()
root.title('Script Launcher')
rootWidth = 768
rootHeight = 768
root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
root.resizable(width=False, height=False)
frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box00 = TK.Listbox(frame00, bd=0)
box10 = TK.Listbox(frame00, bd=0)
box00.grid(row=0, column=0, sticky=TK.N)
box10.grid(row=1, column=0, sticky=TK.S)
frame00.grid(row=0, column=0, sticky=TK.W)
frame00.rowconfigure(0, weight=1)
frame00.rowconfigure(1, weight=1)
frame00.columnconfigure(0, weight=1)
frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box01 = TK.Listbox(frame01, bd=0)
box11 = TK.Listbox(frame01, bd=0)
box01.grid(row=0, column=1, sticky=TK.N)
box11.grid(row=1, column=1, sticky=TK.S)
frame01.grid(row=0, column=1, sticky=TK.E)
frame01.rowconfigure(0, weight=1)
frame01.rowconfigure(1, weight=1)
frame01.columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
for name in range(20)
box00.insert(TK.END, 'test')
box10.insert(TK.END, 'test')
box01.insert(TK.END, 'test')
box11.insert(TK.END, 'test')
root.mainloop()