清除tkinter错误中的输入框值

时间:2018-05-25 02:50:33

标签: python python-3.x tkinter

我想让tkinter在每次按下按钮时清除一个输入框

我希望我的程序如何工作是这样的。每当用户选择统计分布时,右侧会出现一行可变输入框(变量输入框的数量因统计分布而异。我希望用户能够多次更改输入框的值。变量然后在脚本中输入计算。

目前,输入框仅存储并返回用户输入的第一个值。因此,我想在每次调用最终计算之前清除输入框。我知道我需要使用variablename.delete(0,END)命令,但我的实现有问题。我可以很好地清理入口箱,但出于某种原因,每当我将这种语法插入代码时,我的标签语法就会停止工作。

以下是我目前的代码。我已经包含了2个独立的代码块。第一个显示我目前使用variablename.delete(0,END)方法修复问题的尝试。删除方法工作正常,但由于某种原因

import tkinter as tk
from tkinter.ttk import *
from scipy.stats import *
import numpy as np
import pandas as pd


master = tk.Tk()
master.title("Gas Calculator")
v = tk.IntVar()
combo = Combobox(master)
#possible entry boxes depending on the statistic distribution chosen
f1 = tk.Entry(master)
f2 = tk.Entry(master)
f3 = tk.Entry(master)
f4 = tk.Entry(master)
f5 = tk.Entry(master)
f6 = tk.Entry(master)
f7 = tk.Entry(master)
f8 = tk.Entry(master)
f1.grid(row=3, column=5)
f2.grid(row=3, column=6)
f3.grid(row=3, column=7)
f4.grid(row=3, column=8)
f5.grid(row=3, column=9)
f6.grid(row=3, column=10)
#calls the users distribution selection and then is supposed to format the boxes accordinly
def callbackARS(eventObject):

    ARDist=(comboARS.get())

    try:
        if ARDist == "Truncated Normal":
            f1.delete(0,tk.END) 
            f1 = tk.Entry(master)
            f2 = tk.Entry(master)
            f3 = tk.Entry(master)
            f4 = tk.Entry(master)
            f5 = tk.Entry(master,state='disabled')
            f6 = tk.Entry(master,state='disabled')

            f1.grid(row=3, column=5)
            f2.grid(row=3, column=6)
            f3.grid(row=3, column=7)
            f4.grid(row=3, column=8)
            f5.grid(row=3, column=9)
            f6.grid(row=3, column=10)

            tk.Label(master, text="Mean Value", padx=20, width=10, bg = "light blue").grid(row=2,column=5)
            tk.Label(master, text="Standard Deviation", padx=20, width=10, bg = "light blue").grid(row=2,column=6)
            tk.Label(master, text="Low Limit", padx=20, width=10, bg = "light blue").grid(row=2,column=7)
            tk.Label(master, text="High Limit", padx=20, width=10, bg = "light blue").grid(row=2,column=8)
            tk.Label(master, text="", padx=20, width=10).grid(row=2,column=9)
            tk.Label(master, text="", padx=20, width=10).grid(row=2,column=10)


        return test
        return test2
    except:
        pass   

#function to allow users to select what distribution they want
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)



#testing function to check and make sure the input boxes are being cleared. needs to be used in cuncjuntion with the button coded below.
def testing():

    test1 = float(f1.get())
    test2 = float(f2.get())
    testsum = test1 * test2
    print (testsum)
tk.Button(master, text='Calculate', command=testing, width=20).grid(row=35, column=1, sticky=tk.W, pady=4)

如果我能澄清任何事情,请告诉我。

0 个答案:

没有答案