繁体   English   中英

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

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

我正在学习Python,并且开始学习如何使用TKinter GUI。 我正在做一个小界面,可以进行一些简单的统计分析,例如STDEV,T检验等。我在类中拥有此方法,该类基本上可以处理数据。

我希望用户能够输入他们想要的尽可能多的数据条目(我的意思是只要计算机可以处理)。 我遇到的问题是-我认为当我在条目上使用方法.get()时,没有返回值吗?

我还使用DoubleVar()的.trace()方法跟踪使用以下所示方法更新条目值的时间:

条目更改时,Python Tkinter更新

我认为这很有意义,但对我不起作用。 每当我在TK界面中更改一个框时,所有其他框都会更改,但是用于计算标准偏差的值不是“输入”框上显示的数字(无论如何都相同)。

这是代码:

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 

我不确定如果有人可以帮助我,我在这里错了,我真的会很感激。 谢谢!

由于缩进功能已关闭,并且某些按钮调用了不存在的函数,因此无法运行您发布的代码,因此,这是一个标准的跟踪程序,该程序显示了如何使用与跟踪关联的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