Home > other >  Python - tkinter - put different grids in some window?
Python - tkinter - put different grids in some window?

Time:02-17

################################################
#        Left           |         Right        #
#-----------------------|----------------------#
# hello|  hello  |      | hello|  hello | hello#
#-----------------------|------|--------|------#
#                       | hello|               #
################################################

I want to generate some tkinter window of the kind above. Left and Right are two buttons. If I click on Left a new widget window opens with a button apply. If I click on apply the program should remember from which button I opened the Window with apply button and order the word hello in some grid under left (if the Left button was pressed first) or right (if the Right button was pressed first).

My program at moment looks like that:

################################################
#        Left           |         Right        #
#-----------------------|----------------------#
#       hello           |         hello        #
#-----------------------|----------------------#
#                       |         hello        #
################################################

How can I change the style from the second solution to the first?

My code for solution two:

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()

def new_Editor(key):
    def make_label1(lbl_txt):
        print("root.grid_slaves(0):", root.grid_slaves(column=0))
        row = len(root.grid_slaves(column=0)) 1
        lbl = Label(root, text=lbl_txt)
        lbl.grid(row=row, column=0)
    def make_label2(lbl_txt):
        print("root.grid_slaves(1):", root.grid_slaves(column=1))
        row = len(root.grid_slaves(column=1)) 1
        lbl = Label(root, text=lbl_txt)
        lbl.grid(row=row, column=1)

    if key == 1:
        a = NewWindow(root, make_label1)
    else:
        a = NewWindow(root, make_label2)

root.title("BasicWindow")

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)

root.mainloop()

CodePudding user response:

I would suggest to create two frames for the labels, one for the left and the other for the right.

Then you can use the number of labels in each frame to determine the row and column of the new label to be added.

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()

Result:

enter image description here

  • Related