动态添加和删除输入框Tkinter

时间:2018-05-20 04:23:01

标签: python python-3.x tkinter

我正在努力制作一个gui,根据用户选择的统计分布类型为用户提供一系列输入框。这将在稍后的脚本中用于计算。每个统计分布都需要不同数量的变量(因此需要动态填充多个输入框)。现在我的代码有点工作。问题是,如果您选择一个分配,为您提供5个输入框,然后将分布切换为仅需要3的分布,则前一个选择中的5个框仍然存在。对于这个脚本,截断的正态分布应该有3个变量,正态分布应该有5个变量,离散分布应该有4个变量。

我认为如果我创建了一个虚拟输入框然后在if语句的末尾删除它,它最终会删除我不需要的框。这似乎没有奏效。

以下是我到目前为止的代码。我觉得我差不多就在那里,我只是错过了一些东西。我非常感谢任何建议。同样,目标是为每个统计分布选择填充正确的输入框,然后能够动态地更改该选择,并相应地改变输入框的数量。

import tkinter as tk
from tkinter.ttk import *

master = tk.Tk()
master.title("Gas Calculator")
v = tk.IntVar()
combo = Combobox(master)

def callbackARS(eventObject):
    ARDist=(comboARS.get())


    if ARDist == "Truncated Normal":
        a1 = tk.Entry(master)
        a2 = tk.Entry(master)
        a3 = tk.Entry(master)
        #attempt to create a dummy input box that isnt needed and then delete it at the end of the code
        a4 = tk.Entry(master)
        a1.grid(row=3, column=4)
        a2.grid(row=3, column=5)
        a3.grid(row=3, column=6)
        a4.grid(row=3, column=7)
        tk.Label(master, text="Variable", padx=20, width=10, bg = "light blue").grid(row=2,column=4)
        tk.Label(master, text="Area", padx=20, width=10, bg = "light blue").grid(row=2,column=5)
        tk.Label(master, text="Thickness", padx=20, width=10, bg = "light blue").grid(row=2,column=6)
        tk.Label(master, text="", padx=20, width=10, bg = "light blue").grid(row=2,column=7)
        #attempt to create a dummy input box that isnt needed and then delete it at the end of the code
        a4.grid_remove()

    if ARDist == "Normal":
        b1 = tk.Entry(master)
        b2 = tk.Entry(master)
        b3 = tk.Entry(master)
        b4 = tk.Entry(master)
        b5 = tk.Entry(master)
        b1.grid(row=3, column=4)
        b2.grid(row=3, column=5)
        b3.grid(row=3, column=6)
        b4.grid(row=3, column=7)
        b5.grid(row=3, column=8)
        tk.Label(master, text="a", padx=20, width=10, bg = "light blue").grid(row=2,column=4)
        tk.Label(master, text="Aa", padx=20, width=10, bg = "light blue").grid(row=2,column=5)
        tk.Label(master, text="aaa", padx=20, width=10, bg = "light blue").grid(row=2,column=6)
        tk.Label(master, text="aaaa", padx=20, width=10, bg = "light blue").grid(row=2,column=7)

    if ARDist == "Discrete":
        a1 = tk.Entry(master)
        a2 = tk.Entry(master)
        a3 = tk.Entry(master)
        a4 = tk.Entry(master)
        a1.grid(row=3, column=4)
        a2.grid(row=3, column=5)
        a3.grid(row=3, column=6)
        a4.grid(row=3, column=7)

#Distribution selction
comboARS = Combobox(master)
comboARS['values']= ("Truncated Normal", "Normal", "Discrete")
comboARS.current(0) #set the selected item
comboARS.grid(row=3, column=3)
comboARS.bind("<<ComboboxSelected>>", callbackARS)

1 个答案:

答案 0 :(得分:0)

如果在框架中创建一组标签和输入字段,则可以轻松操纵其属性和可见性。 grid_remove()和winfo_children()方法的组合非常有用。

import tkinter as tk
from tkinter import ttk

master = tk.Tk()
my_frame = tk.Frame()
my_frame.grid(row=0, column=1)
# create one set of widgets in a frame
for num in range(1, 6):
    tk.Label(master=my_frame).grid(row=0,column=num)
    tk.Entry(master=my_frame).grid(row=1, column=num)
def detect_selection(eventobject):
    the_selection = eventobject.widget.get()
    # hide all widgets
    for item in my_frame.winfo_children():
        item.grid_remove()
    show_idx__wigets = 6
    if the_selection == '3':
        change_labeltext(('a', 'b', 'c'), 6)
    if the_selection == '4':
        show_idx__wigets = 8
        change_labeltext(('d', 'e', 'f', 'g'), 8)
    if the_selection == '5':
        show_idx__wigets = 10
        change_labeltext(('h', 'i', 'j', 'k', 'l'), 10)
    # reveal only desired number of widgets
    show_widgets(show_idx__wigets)

def show_widgets(upto_widgetposition):
    for item in my_frame.winfo_children()[:upto_widgetposition]:
        item.grid()
def change_labeltext(labeltext, upto_widgetposition):
    for txt, item in zip(
        labeltext, my_frame.winfo_children()[:upto_widgetposition:2]):
        item['text'] = txt    
combo = ttk.Combobox(master,
                     values=(3,4,5))
combo.grid(row=0, column=0, sticky='s')
combo.bind('<<ComboboxSelected>>', detect_selection)

master.mainloop()