代码之家  ›  专栏  ›  技术社区  ›  Martin Kunze

Pythin-tkinter-在某个窗口中放置不同的网格?

  •  0
  • Martin Kunze  · 技术社区  · 3 年前
    ################################################
    #        Left           |         Right        #
    #-----------------------|----------------------#
    # hello|  hello  |      | hello|  hello | hello#
    #-----------------------|------|--------|------#
    #                       | hello|               #
    ################################################
    

    我想生成一些以上类型的tkinter窗口。“左”和“右”是两个按钮。如果我单击左侧,将打开一个新的窗口小部件,其中包含一个应用按钮。如果我点击应用,程序应该记得我用应用按钮打开窗口的哪个按钮,并在左(如果先按左按钮)或右(如果先按下右按钮)下的某个网格中排列单词hello。

    我现在的程序是这样的:

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

    如何将样式从第二个解决方案更改为第一个解决方案?

    我的解决方案二代码:

    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()
    
    0 回复  |  直到 3 年前
        1
  •  2
  •   acw1668    3 年前

    我建议为标签创建两个框架,一个用于 左边 另一个用于 正确的

    然后,您可以使用每帧中的标签数量来确定 一行 要添加的新标签的。

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

    结果:

    enter image description here