简体   繁体   English

当用户继续在TKinter中更改值(使用跟踪方法)时,使用Entry并更新输入的条目

[英]Using Entry and updating an entry entered, as the user continues to change the values (using trace method) in TKinter

I am learning Python and I am starting to learn how to use the TKinter GUI. 我正在学习Python,并且开始学习如何使用TKinter GUI。 I am making a small interface that does some simple statistical analysis like STDEV, T-tests, etc. I have this method in a class which basically gets the data to work with. 我正在做一个小界面,可以进行一些简单的统计分析,例如STDEV,T检验等。我在类中拥有此方法,该类基本上可以处理数据。

I want the user to be able enter as many data entries as they want (I mean as long as the computer can handle of course). 我希望用户能够输入他们想要的尽可能多的数据条目(我的意思是只要计算机可以处理)。 The problem I am having is -- I think when I use the method .get() on an Entry, None is returned? 我遇到的问题是-我认为当我在条目上使用方法.get()时,没有返回值吗?

I am also using method .trace() of DoubleVar() to trace when the values of the entries are updated using the method shown here: 我还使用DoubleVar()的.trace()方法跟踪使用以下所示方法更新条目值的时间:

Python Tkinter update when entry is changed 条目更改时,Python Tkinter更新

I thought it made sense but it's not working for me. 我认为这很有意义,但对我不起作用。 Whenever I change a box in my TK interface, all the other boxes get changed, but the values used to calculate standard deviation are not the numbers that are being shown on the Entry boxes (which are all the same anyways). 每当我在TK界面中更改一个框时,所有其他框都会更改,但是用于计算标准偏差的值不是“输入”框上显示的数字(无论如何都相同)。

Here is the code: 这是代码:

class StandardDeviation: """ Runs standard deivation calculations. """ class StandardDeviation:“”“运行标准偏差计算。”“”

def __init__(self) -> None:
    """
    Initializes an instance of the functions!
    """
    self.stdev_pop = Button(top_frame,
                            text="Calculate the population "
                                 "standard deviation of the data set")
    self.stdev_pop.bind("<Button-1>", self.show_result_population)
    self.stdev_pop.pack()
    stdev_samp = Button(top_frame,
                        text="Calculate the sample "
                             "standard deviation of the data set")
    stdev_samp.bind("<Button-1>", self.show_result_sample)
    stdev_samp.pack()
    self.data = []
    self.enter_data = Button(top_frame, text="Enter data")
    self.enter_data.bind("<Button-1>", self.pack_add_entry_button)

    self.add_entry = Button(top_frame, text="Add data entry",
                            command=self.add_new_entry)
    self.enter_data.pack()
    self.all_entries = {}
    self.tracer = DoubleVar()
    self.tracer.trace("w", self.update)

def pack_add_entry_button(self, *args) -> None:
    """
    Pack the add_entry button.
    """
    self.add_entry.pack()

def update(self, *args) -> None:
    """
    Update the values of the entries.
    """
    global update_in_progress
    if update_in_progress:
        return
    update_in_progress = True
    data = [str(self.all_entries[item]) for item in self.all_entries]
    self.data = [int(item) for item in data if item.isnumeric()]
    update_in_progress = False

def add_new_entry(self):
    """
    Add a new entry.
    """
    new_entry = Entry(root, textvariable=self.tracer)
    new_entry.pack()
    new_entry_data = new_entry.get()
    self.all_entries[new_entry] = new_entry_data 

I'm not sure where I'm wrong here if anyone could help me I'd really appreciate it. 我不确定如果有人可以帮助我,我在这里错了,我真的会很感激。 Thank you! 谢谢!

There is no way to run the code you posted as the indentation is off and some of the buttons call functions that don't exist so this is a standard trace program that shows how to use the tkinter variable associated with the trace, a StringVar in ths case, to get the contents. 由于缩进功能已关闭,并且某些按钮调用了不存在的函数,因此无法运行您发布的代码,因此,这是一个标准的跟踪程序,该程序显示了如何使用与跟踪关联的tkinter变量,即StringVar在这种情况下,获取内容。

import tkinter

def text_changed(*args):
    print(tk_name.get())

top = tkinter.Tk()

tk_name=tkinter.StringVar()
tk_name.set("nothing")
tk_name.trace("w", text_changed)

tkinter.Label(top, textvariable=tk_name).grid(row=0, column=1)

entry_1 = tkinter.Entry(top, textvariable=tk_name)
entry_1.grid(row=1, column=1, sticky="W")
entry_1.focus_set()

top.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM