我建议为标签创建两个框架,一个用于
左边
另一个用于
正确的
。
然后,您可以使用每帧中的标签数量来确定
一行
和
柱
要添加的新标签的。
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None, apply=None):
super().__init__(master = master)
self.title('NewWindow')
self.master = master
self.words = 'Hello'
self.bt1 = Button(self, text="apply", command=self.bt_press)
self.bt1.grid(column=0, row=0)
self.apply = apply
def bt_press(self):
self.apply(self.words)
self.destroy()
root = Tk()
# adjust the below two settings to suit your requirement
COLS = 3
COLWIDTH = 50
def new_Editor(key):
def make_label1(lbl_txt):
# get the children of left frame
slaves = root.left_frame.grid_slaves()
print("root.left_frame.grid_slaves():", slaves)
# determine the row and column of the new label
row, col = divmod(len(slaves), COLS)
lbl = Label(root.left_frame, text=lbl_txt)
lbl.grid(row=row, column=col)
def make_label2(lbl_txt):
# get the children of right frame
slaves = root.right_frame.grid_slaves()
print("root.right_frame.grid_slaves():", slaves)
# determine the row and column of the new label
row, col = divmod(len(slaves), COLS)
lbl = Label(root.right_frame, text=lbl_txt)
lbl.grid(row=row, column=col)
if key == 1:
a = NewWindow(root, make_label1)
else:
a = NewWindow(root, make_label2)
root.title("BasicWindow")
# make the two column same size
root.columnconfigure((0,1), weight=1, uniform=1, minsize=COLS*COLWIDTH)
root.basic_bt_l = Button(root, text="Left", command=lambda: new_Editor(1))
root.basic_bt_l.grid(column=0, row=0)
root.basic_bt_r = Button(root, text="Right", command=lambda: new_Editor(2))
root.basic_bt_r.grid(column=1, row=0)
# frame for left labels
root.left_frame = Frame(root)
root.left_frame.grid(row=1, column=0, sticky='nsew')
# make all the columns inside the frame same size
root.left_frame.columnconfigure(list(range(COLS)), weight=1, uniform=1, minsize=COLWIDTH)
# frame for right labels
root.right_frame = Frame(root)
root.right_frame.grid(row=1, column=1, sticky='nsew')
# make all the columns inside the frame same size
root.right_frame.columnconfigure(list(range(COLS)), weight=1, uniform=1, minsize=COLWIDTH)
root.mainloop()
结果: