动态地将tkinter窗口适合其内容

时间:2017-07-20 11:08:02

标签: python tkinter

我有一个Toplevel窗口,其中一个网格行包含一个Label,Entry和一个“+”按钮(window on startup) 当我点击添加按钮时,会生成具有相同内容的新行。但问题是,窗口没有调整大小并适合其新内容。应该看起来像resized window

代码如下:

def process_map():
    numbers = {0:'\u2080', 1:'\u2081', 2:'\u2082', 3:'\u2083', 4:'\u2084', 5:'\u2085', 6:'\u2086', 7:'\u2087', 8:'\u2088', 9:'\u2089'}
    button_pos = tk.IntVar(0)
    ENTRIES = {}

    def add_button():
        if button_pos.get() >= 10:
            index = numbers[button_pos.get()//10] + numbers[button_pos.get()%10]
        else:
            index = numbers[button_pos.get()]
        lb = tk.Label(top_root, text='\u03C6'+index)
        lb.grid(row=button_pos.get(), column=0, sticky='NWES')
        entry = tk.Entry(top_root, width=4, relief='sunken', bd=2)
        entry.grid(row=button_pos.get(), column=1, sticky='WE', padx=5, pady=5)
        ENTRIES.update({button_pos.get():entry})
        bt.grid(row=button_pos.get(), column=2, sticky='WE', padx=5, pady=5)
        bt_close.grid(row=button_pos.get()+1, column=1, padx=5, pady=5)
        bt_start.grid(row=button_pos.get()+1, column=0, padx=5, pady=5)
        button_pos.set(button_pos.get()+1)
        center(top_root)

    top_root = tk.Toplevel(root)
    top_root.title('Select \u03C6')
    lb = tk.Label(top_root, text='\u03C6\u2081', height=1)
    lb.grid(row=0, column=0, sticky='NWES')

    entry = tk.Entry(top_root, width=4, relief='sunken', bd=2)
    entry.grid(row=0, column=1, sticky='WE', padx=5, pady=5)
    button_pos.set(button_pos.get()+2)
    ENTRIES.update({button_pos.get():entry})

    bt = tk.Button(top_root, text='+', command=add_button,)
    bt.grid(row=0, column=2, sticky='WE', padx=5, pady=5)

    bt_close = tk.Button(top_root, text='Cancel', width=15, command=top_root.destroy)
    bt_close.grid(row=button_pos.get()+1, column=1, padx=5, pady=5)
    bt_start = tk.Button(top_root, text='Start', width=15)
    bt_start.grid(row=button_pos.get()+1, column=0, padx=5, pady=5)
    center(top_root)
    top_root.mainloop()

0 个答案:

没有答案